打卡第四天。加油。。。。
第四天发现,复习完之前学的,啥也没做。。。。
1、Spring注解开发
1)Spring原始注解主要是替代< bean >的配置
| 注解 | 说明 | 
|---|---|
| @Component | 使用在类上用于实例化Bean | 
| @Controller | 使用在web层类上用实例化Bean | 
| @Service | 使用在service层类上用于实例化Bean | 
| @Repository | 使用在dao层类上用于实例化Bean | 
| @Autowired | 使用在字段上用于根据类型依赖注入 | 
| @Qualifier | 结合@Autowired一起使用用于根据名称进行依赖注入 | 
| @Resource | 相当于@Autowired+@Qualifier,按照名称进行注入 | 
| @Value | 注入普通属性 | 
| @Scope | 注入Bean的作用范围 | 
| @PostConstruct | 使用在方法上标注该方法时Bean的初始化方法 | 
| @PreDestroy | 使用在方法上标注该方法时Bean的销毁方法 | 
UserDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11package com.wwq.dao;
import org.springframework.stereotype.Component;
// <bean id="userDao" class="com.wwq.dao.UserDaoImpl"/>
("userDao")
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("save Running....");
}
}UserServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package com.wwq.service;
import com.wwq.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
// <bean id="userService" class="com.wwq.service.UserServiceImpl">
("userService")
public class UserServiceImpl implements UserService {
// <property name="userDao" ref="userDao"/>
//如果只按照类型注入则,只写@Autowired就行,如果需要按照名称注入则需要加上@Qualifier
("userDao")
// 相当于@Autowired+Qualifier
// @Resource
private UserDao userDao;
//xml方式必须要写set方法,注解方式可以省略
//public void setUserDao(UserDao userDao){
// this.userDao = userDao;
//}
public void save() {
userDao.save();
}
}applicationContext.xml
1
2<!-- 配置组件扫描-->
<context:component-scan base-package="com.wwq"/>
2)Spring的新注解
| 注释 | 说明 | 
|---|---|
| @Configuration | 用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注释 | 
| @ComponentScan | 用于指定Spring在初始化容器时要扫描的包。作用和在Spring的xml配置文件中的<context:component-scan base-package=”com.itheima”/>一样 | 
| @Bean | 使用在方法上,标注将该方法的返回值存储至Spring容器中 | 
| @PropertySource | 用于加载properties文件中的配置 | 
| @import | 用于导入其他配置类 | 
需要替换的注解包括:
- 非定义的Bean的配置:< bean >
 - 加载properties文件的配置:< context:property-placeholder >
 - 组件扫描的配置:< context:component-scan >
 引入其他文件:< import >
SpringConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.wwq.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import java.beans.PropertyVetoException;
//标志该类是Spring的核心配置文件
//<context:component-scan base-package="com.wwq"/>
("com.wwq")
//<context:property-placeholder location="classpath:jdbc.properties"/>
("classpath:jdbc.properties")
({DataSourceConfiguration.class})
public class SpringConfiguration {
}DataSourceConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package com.wwq.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import java.beans.PropertyVetoException;
//<context:property-placeholder location="classpath:jdbc.properties"/>
("classpath:jdbc.properties")
public class DataSourceConfiguration {
("${jdbc.driver}")
private String driver;
("${jdbc.url}")
private String url;
("${jdbc.username}")
private String username;
("${jdbc.password}")
private String password;
("dataSource")//Spring会将当前方法的返回值以指定名称,存储到Spring容器中
public ComboPooledDataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}- 测试代码(UserController.java)
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.wwq.web;
import com.wwq.config.SpringConfiguration;
import com.wwq.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class UserController {
public static void main(String[] args) {
//ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
2、Spring整合Junit
1、Spring集成Junit步骤
- 导入spring集成Junit的坐标
 - 使用@Runwith注解替换原来的运行期
 - 使用@ContextConfiguration指定配置文件或配置类
 - 使用@Autowired注入需要测试的对象
 - 创建测试方法进行测试