Spring-基于注解配置bean
文章目录
一. 基于注解配置bean
使用注解可以提高开发效率,但是维护起来比较痛苦。目前主流框架均支持注解和xml配置两种开发方式。
由于历史原因,某些项目常用 xml 配置 bean,使用 注解 导入 bean之间的依赖。
1.1 准备工作
在核心配置文件中,相较于xml配置的形式,注解需要额外添加一些东西,如下所示:
其中第三行、第六,七行为新增内容,是context及其约束。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启Spring的注解功能-->
<context:annotation-config/>
<!--
配置注解所在的类的包名,让Spring可以通过这些包名去找到对应的添加注解的类
base-package下的所有包均可以使用注解
如果没有配置开启Spring的注解功能,但是配置了Spring 的扫描包,也会自动开启注解功能,也即有这句话,上面的就不用写了
-->
<context:component-scan base-package="org.buaa"/>
</beans>
1.2 使用注解
首先在任何需要Spring管理的Bean上加@component
。
依然模拟web流程,service调用dao
OrderDao文件
@Component
public class OrderDao {
public void addOrder(){
System.out.println("添加订单");
}
}
OrderService文件
// 使用@Component等价于在xml中配置 <bean id = "OrderService" class = "org.buaa.service.OrderService">
@Component
public class OrderService {
// @Autowired,自动装配,用在属性注入上
@Autowired
private OrderDao orderDao;
public void addOrder(){
System.out.println("Service层的添加addOrder");
orderDao.addOrder();
}
}
1.3 分层注解
JaveEE 使用分层开发,每层类都交给 Spring容器 管理,均使用 @Component,那么不容易区分当前注解的 bean 属于哪一层。因此衍生出三个注解
- @Component:任何一个java类均可以使用这个注解,只要被它标注,Spring就会以bean的方式管理这个类
- @Controller:主要用于标注 web层 的类, 如LoginServlet类
- @Service:主要用于标注 service层 的类,如UserService类
- @Repository:主要用于标注 dao层 的类,如UserDao类
1.4 @Component起别名
使用@Component可以起别名,如下
@Component("CarCar")
public class Car{
}
二. Bean属性依赖注入(注解方式)
2.1 简单数据注入
只需要在对应的成员变量上或者set方法前加上 @Value注解 即可。
@Component
public class Person {
@Value("javak")
private String name;
private String pwd;
public void setName(String name) {
this.name = name;
}
@Value("123")
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2.2 复杂类型注入
@Autowired注解就可以做到
@Component
public class Person {
private String name;
private String pwd;
// 使用@Autowired直接注入某个bean,当然这个bean也需要被@Component
@Autowired
private Car car;
...
}
如果bean使用的不是默认名称,那么需要更加复杂的注解 @Qualifier 来指定bean的名称
@Component
public class Person {
...
// 使用@Autowired直接注入某个bean,当然这个bean也需要被@Component
@Autowired
@Qualifier("CarCar")
private Car car;
...
}
@Component("CarCar")
public class Car{
}
2.3 注解配置bean 的初始化和销毁方法
和xml配置相近,可以使用注解@PostConstruct, @PostDestory 来配置初始化、销毁时执行的方法。
@Component
public class Person {
...
@PostConstruct
public void init(){
}
@PostDestory
public void Destory(){
}
...
}
注意,虽然 @PostConstruct, @PostDestory 常用于Spring,但是它时JavaEE的注解,属于包javax.annotation.Resource。所以想要使用的话,需要在pom.xml中导入如下:
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
2.4 注解配置 bean 的作用域
使用 @Scope 注解配置作用域,值为singleton为单例, prototype 为多例
@Component
@Scope("prototype")
public class Person {
...
}
三. spring整合其他框架,举例junit
使用注解省去new context对象
使用时如下:使用 @RunWith 来整合junit, 使用@ContextConfiguration导入核心配置文件
如此一来,使用时就不需要自己new ClassPathXmlApplicationContext对象了。可以使用注解 @Autowired 来自动装配所需使用的bean,直接使用其方法即可。
// 当前需要使用Spring和junit整合包中的核心对象,将Spring和junit整合到一起使用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Test2 {
@Autowired
private OrderService orderService;
@Autowired
private Person person;
@Test
public void demo(){
orderService.addOrder();
System.out.println(person);
}
}
spring整合其他框架时,要导入 spring-其他框架.jar 包。这个是桥梁jar包,大部分情况是spring提供的,有时是其他人提供(这种情况下,一般是民间组织搞得框架)。
举例:spring与junit框架整合,需要导入spring-test包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
<scope>test</scope>
</dependency>
总结:
配置
<!--开启Spring的注解功能-->
<context:annotation-config/>
<!-- 配置注解所在的类的包名 -->
<context:component-scan base-package="org.buaa"/>
常见注解
- @Component: 在被管理的Bean上使用,等价于配置
- @Controller:@Component的衍生注解,主要用于标注 web层 的类, 如LoginServlet类
- @Service:@Component的衍生注解,主要用于标注 service层 的类,如UserService类
- @Repository:@Component的衍生注解,主要用于标注 dao层 的类,如UserDao类
- @Autowired: 自动装配,用在成员变量上,用于复杂类型注入;
- @Value:用于成员变量、set方法上,用于数据注入
- @Qualifier:如果bean使用的不是默认名称,那么需要注解 @Qualifier 来指定bean的名称
- @PostConstruct:用于方法上,该方法会在初始化时执行
- @PostDestory:用于方法上,该方法会在销毁时执行
- @Scope:用在bean上,声明作用域,值为singleton为单例, prototype 为多例
- @RunWith(SpringJUnit4ClassRunner.class):用于整合Spring和junit
- @ContextConfiguration(“classpath:applicationContext2.xml”):用于导入核心配置文件