1. 常用注解
xml配置spring
<bean id="accountService" class="cn.bruce.service.impl.AccountServiceImpl"
scope="" init-method="" destroy-method="">
<property name="" value="" | ref=""></property>
</bean>
复制代码
1.1 用于创建对象的注解
此类注解的作用就和在XML配置文件中编写一个<bean>
标签实现的功能是一样的,
相当于xml配置中的<bean id = "" class = "">
@Component
用于把当前类对象存入spring容器中
属性:value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母小写。
@Controller
一般用在表现层
@Service
一般用在业务层
@Repository
一般用在持久层
以上三个注解他们的作用和属性与Component是一模一样,可以不按照规定写注解。他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰。
springIoC容器在创建对象时使用的空参构造器,因此在书写类的时候要提供空参构造器,不然会Bean异常
NoSuchBeanDefinitionException
1.2 用于注入数据的注解
此类注解的作用就和在xml配置文件中的bean标签中写一个<property>
标签的作用是一样的。
相当于xml配置中的<property name = "" ref = "">
@Autowired
自动按照类型注入。
- 如果容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。
- 如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
- 如果Ioc容器中有多个类型匹配时:Autowired在进行匹配时会进行类型匹配,如果有唯一的类型匹配,则进行直接注入。如果IoC中有多个类型匹配,会首先按照类型确定匹配对象,接下来使用变量名称作为Bean的id在确定的范围内继续查找,如果找到则注入成功,如果找不到则报错。
注解位置:可以是变量上,也可以是方法上。
在使用注解注入时,set方法就不是必须的了。
@Qualifier
在按照类中注入的基础之上再按照名称注入,需要和@Autowired
注解配合使用。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以。
属性:value:用于指定注入bean的id。
@Resource
直接按照bean的id注入。它可以独立使用,通过名称Bean名称进行依赖注入。 属性:name:用于指定bean的id。
以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。集合类型的注入只能通过XML来实现。
@Value
用于注入基本类型和String类型的数据
属性:value:用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)
SpEL的写法:${表达式}
1.3 用于修改作用范围的注解
此注解的作用就和在bean标签中使用scope属性实现的功能是一样的
相当于xml配置中的<bean id = "" class = "" scope = "">
@Scope
作用:用于指定bean的作用范围
属性:value:指定范围的取值。常用取值:singleton
单例的 prototype
多例的
1.4 与声明周期相关的注解
此类注解的作用就和在bean标签中使用init-method
和destroy-methode
的作用是一样的
相当于xml配置中的<bean id = "" class = "" init-method = "" destroy-methode = "">
@PostConstruct
用于指定初始化方法
@PreDestroy
用于指定销毁方法
代码示例:
@Service
//@Scope("prototype")
public class AccountServiceImpl implements AccountService {
// @Autowired
// @Qualifier("accountDao1")
@Resource(name = "accountDao2")
private AccountDao accountDao;
@Value(value = "Bruce")
private String name;
@Value("30")
private Integer age;
@Autowired
private Date birthday;
@PostConstruct
public void init() {
System.out.println("初始化方法执行。。。。");
}
@PreDestroy
public void destory() {
System.out.println("销毁方法执行。。。。");
}
public void saveAccount() {
System.out.println("Service创建的账户保存了");
accountDao.saveAccount();
}
public void show() {
System.out.println(name + " " + age + " " + birthday);
}
}
复制代码
注解中有且只有一个属性要赋值,且名称是value的时候可以不写value,但是多个属性时必须写value
1.5 spring中xml配置和注解之间的关系
注解配置优势:配置简单,易于维护(配置都在对应的类上)
xml配置的优势:修改时不需要重新编译,通过外部引入
spring中Bean管理方式的对比
xml配置 | 注解配置 | |
---|---|---|
Bean定义 | <bean id = "" class = ""> |
@Component @Service @Controller @Repository |
Bean名称 | 通过id或者name指定 | @Component("XXX") |
Bean注入 | <property> 或者P命名空间 |
@Autowired 类型注入 @Qualifier 名称注入 |
Bean生命周期、作用范围 | 作用范围通过scope 属性设置,init-method destroy-method |
@Scope 设置作用范围 @PostConstruct 初始化 @PreDestroy 销毁 |
适合的场景 | Bean来自第三方时 | Bean的实现类由自己开发 |
基于注解的spring IoC配置中,bean对象的特点和基于xml配置是一样的
2. 新的注解-与配置相关
@Configuration
指定当前类是一个配置类,当容器创建时会从该类上加载注解。获取容器时需要使用AnnotationConfigApplicationContext(有@Configuration
注解的类.class)
当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
@ComponentScan
用于通过注解指定spring在创建容器时要扫描的包。
等同于xml中 <context:component-scan base-package="cn.bruce"></context:component-scan>
属性: value
和basePackages
的作用是一样的,都是用于指定创建容器时要扫描的包。
在进行包扫描的时候首先要确实此类是配置类,才会进行包扫描。
@Bean
此注解只能写在方法上,用于把当前方法的返回值作为bean对象存入spring的ioc容器中
属性: name:用于指定bean的id。当不写时,默认值是当前方法的名称
当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。查找的方式和Autowired注解的作用是一样的
@Import
用于导入其他的配置类,是个数组可以导入很多配置类
属性:value[]:用于指定其他配置类的字节码。
当我们使用Import的注解之后,有Import注解的类就父配置类,而导入的都是子配置类
@PropertySource
用于指定properties文件的位置。 属性: value:指定文件的名称和路径。
关键字:classpath,表示类路径下
通过注解获取IoC容器:
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
复制代码
3. 注解配置和XML配置对比
3.1 xml配置
书写在src下的resources中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/base_crud"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 配置QueryRunner -->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
<!-- 注入数据源 -->
<constructor-arg name="ds" value="dataSource"></constructor-arg>
</bean>
<!-- 配置Dao -->
<bean id="accountDao" class="cn.bruce.dao.impl.AccountDaoImpl">
<!-- 注入QueryRunner -->
<property name="runner" ref="runner"></property>
</bean>
<!-- 配置Service -->
<bean id="accountService" class="cn.bruce.service.impl.AccountServiceImpl">
<!-- 注入Dao -->
<property name="accountDao" ref="accountDao"></property>
</bean>
</beans>
复制代码
3.2 注解配置配置
配置代码书写在config包中与项目包平级
SpringConfiguration类
@ComponentScan("cn.bruce")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
复制代码
JdbcConfig类
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 创建数据源对象
* @return
*/
@Bean(name = "ds")
public DataSource creatDataSource(){
ComboPooledDataSource ds = new ComboPooledDataSource();
try {
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
/**
* 创建QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner creatQueryRunner(@Qualifier("ds") DataSource dataSource){
return new QueryRunner(dataSource);
}
}
复制代码
通过IoC容器获取对象
通过依赖注入为对象注入数据
3.3. spring整合Junit单元测试
Junit执行问题:
- 应用程序的入口:main方法
- Junit中单元测试方法,没有main方法,此方法会判断当前测试类中那些方法有
@Test
注解,Junit会选择有@Test
注解的方法执行 - Junit在执行测试方法时,Junit不知道是否使用Spring框架,因此不会读取配置文件/配置类也不会创建IoC容器
- 因此当测试方法执行时没有创建容器,会产生空指针异常
Spring整合Junit:
- 导入spring整合junit的jar(坐标)
- 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的
@Runwith
- 告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且指明位置
@ContextConfiguration
locations:指定xml文件的位置,加上classpath关键字,表示在类路径下 classes:指定注解类所在地位置
代码示例
maven坐标:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
复制代码
test测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private AccountService accountService = null;
@Test
public void testFindAll(){
List<Account> accounts = accountService.findAll();
for(Account account : accounts){
System.out.println(account);
}
}
}
复制代码
注意:
① 当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
② 当通过xml配置spring时,本地路径注意不要有空格
@ContextConfiguration(locations = "classpath:beans.xml")