IOC & DI 配置Bean之注解配置
Bean之注解配置
在 classpath 中扫描组件
- 组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件.
- 特定组件包括:
- @Component: 基本注解, 标识了一个受 Spring 管理的组件
- @Respository: 标识持久层组件
- @Service: 标识服务层(业务层)组件
- @Controller: 标识表现层组件
- 对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
- 当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 <context:component-scan>
目录结构:
实测:
TestObject.java
/**
*
* @ClassName: TestObject
* @Description:测试bean的注解配置形式
* @author: xyc
* @date: 2016年11月27日 下午8:40:12
*
*/
@Component
public class TestObject {
public void test(){
System.out.println("test...");
}
}
UserRespository.java/**
*
* @ClassName: UserRespository
* @Description: 持久层接口
* @author: xyc
* @date: 2016年11月27日 下午8:43:57
*
*/
public interface UserRespository {
public void save();
}
UserRespositoryImpl.java/**
*
* @ClassName: UserRespositoryImpl
* @Description:持久层实现类
* @author: xyc
* @date: 2016年11月27日 下午8:44:17
*
*/
@Repository("userRespository")
public class UserRespositoryImpl implements UserRespository{
@Override
public void save() {
System.out.println("UserRespositoryImpl save...");
}
}
UserService.java/**
*
* @ClassName: UserService
* @Description:业务层实现类
* @author: xyc
* @date: 2016年11月27日 下午8:44:35
*
*/
@Service
public class UserService {
public void add(){
System.out.println("UserService add...");
}
}
UserController.java/**
*
* @ClassName: UserController
* @Description: 控制层
* @author: xyc
* @date: 2016年11月27日 下午8:45:42
*
*/
@Controller
public class UserController {
public void execute(){
System.out.println("UserController execute...");
}
}
bean-annotation.xml<?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-4.3.xsd">
<!-- 配置全局扫描包,指定spring IOC容器扫描的包-->
<context:component-scan base-package="com.xyc.spring.annotation"></context:component-scan>
</beans>
配置之后,可以看到所有.java上面就有了一个S,表示加入了IOC容器中
测试一下:
ApplicationAnnotation.java
/**
*
* @ClassName: ApplicationAnnotation
* @Description:测试利用注解方式配置bean
* @author: xyc
* @date: 2016年11月27日 下午9:19:22
*
*/
public class ApplicationAnnotation {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean-annotation.xml");
UserController userController = (UserController) context.getBean("userController");
System.out.println(userController);
UserService userService = (UserService) context.getBean("userService");
System.out.println(userService);
UserRespository userRespository = (UserRespository) context.getBean("userRespository");
System.out.println(userRespository);
TestObject testObject = (TestObject) context.getBean("testObject");
System.out.println(testObject);
}
}
测试结果:
可以看到,IOC容器中的bean都被加载到了.
<context:component-scan>属性说明:
- base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
- 当需要扫描多个包时, 可以使用逗号分隔.
- 如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern 属性过滤特定的类,示例:
<!-- 指定spring IOC容器扫描的包 可以通过resource-pattern 指定扫描的资源,如下只扫描servcie层下面的 -->
<context:component-scan base-package="com.xyc.spring.annotation" resource-pattern="service/*.class"></context:component-scan>
<context:component-scan>子节点说明:
- <context:include-filter> 子节点表示要包含的目标类
- <context:exclude-filter> 子节点表示要排除在外的目标类
- <context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
<context:include-filter> 和 <context:exclude-filter> 子节点支持多种类型的过滤表达式:
通过annotaion方式过滤 exclude-filter
<!-- context:exclude-filter 子节点指定排除那些指定表达式的组件 如下排除持久层-->
<context:component-scan base-package="com.xyc.spring.annotation">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
通过annotaion方式过滤 include-filter
<!-- context:include-filter 子节点指定包含哪些指定表达式的组件 如下只扫描持久层,但是该子节点需要use-default-filters 配合使用
use-default-filters :默认true,扫描所有 设置为false,不要走默认
-->
<context:component-scan base-package="com.xyc.spring.annotation" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
通过assignable过滤 exclude-filter
<!-- 通过类名的方式过滤 exclude-filter -->
<context:component-scan base-package="com.xyc.spring.annotation">
<context:exclude-filter type="assignable" expression="com.xyc.spring.annotation.service.UserService"/>
</context:component-scan>
通过assignable过滤 include-filter
<!-- 通过类名的方式过滤 include-filter -->
<context:component-scan base-package="com.xyc.spring.annotation" use-default-filters="false">
<context:include-filter type="assignable" expression="com.xyc.spring.annotation.service.UserService"/>
</context:component-scan>
组件装配
使用 @Autowired 自动装配 Bean
<context:component-scan> 元素还会自动注册
AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
- 构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
- 默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
- 默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
- @Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
- @Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
- @Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
@Authwired注解说明:
如下:去掉Component注解,则spring IOC容器就不存在这个bean
/**
*
* @ClassName: TestObject
* @Description:测试bean的注解配置形式
* @author: xyc
* @date: 2016年11月27日 下午8:40:12
*
*/
/*@Component*/
public class TestObject {
public void test(){
System.out.println("test...");
}
}
/**
*
* @ClassName: UserRespositoryImpl
* @Description:持久层实现类
* @author: xyc
* @date: 2016年11月27日 下午8:44:17
*
*/
@Repository("userRespository")
public class UserRespositoryImpl implements UserRespository{
@Autowired(required=false)
private TestObject testObject;
@Override
public void save() {
System.out.println("UserRespositoryImpl save...");
//testObject.test();
}
}
@Qualifier注解说明:
添加类同样实现UserRespository接口,接口下有多个实现类
@Repository
public class TestRespositoryImpl implements UserRespository{
@Override
public void save() {
System.out.println("TestRespositoryImpl save...");
}
}
/**
*
* @ClassName: UserRespositoryImpl
* @Description:持久层实现类
* @author: xyc
* @date: 2016年11月27日 下午8:44:17
*
*/
@Repository
public class UserRespositoryImpl implements UserRespository{
@Autowired(required=false)
private TestObject testObject;
@Override
public void save() {
System.out.println("UserRespositoryImpl save...");
//testObject.test();
}
}
@Service
public class UserService {
@Autowired
@Qualifier("testRespositoryImpl")
private UserRespository userRespository;
public void add(){
System.out.println("UserService add...");
userRespository.save();
}
}
第二种方式指定给@Repository指定bean的名称.
@Autowired
private UserRespository bean的名称;
private UserRespository bean的名称;
使用 @Resource 或 @Inject 自动装配 Bean
- Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
- @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
- @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
- 建议使用 @Autowired 注解