Spring中的组件扫描以及自动装配Bean

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43687990/article/details/102594678

组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件

特定组件包括:

@Component:基本注解,标识一个受Spring管理的组件
@Respository:标识持久层
@Service:标识服务层(业务层)组件
@Controller:标识表现层组件

对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称。

当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明context:component-scan
->base-package属性:指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类,当需要扫描多个包时,可以使用逗号分隔
context:include-filter 子节点表示要包含的目标类
context:exclude-filter 子节点表示要排除在外的目标类
context:component-scan 下可以拥有若干个 context:include-filtercontext:exclude-filter 子节点

context:include-filtercontext:exclude-filter 子节点支持多种类型的过滤表达式
在这里插入图片描述
示例代码

<!-- base-package 属性指定一个需要扫描的基类包,Spring容器会扫描该基类包及其子包下的所有类 -->
	 <context:component-scan base-package="基类包的路径"></context:component-scan> 
	
	<!-- resource-pattern:指定扫描的资源:如下指定的是tow包下的所有类 -->
<!-- 	<context:component-scan base-package="基类包的路径" resource-pattern="tow/*.class"></context:component-scan>
 -->
 	
 	<!--context:exclude-filter type="annotation":子节点指定排除那些指定表达式的组件,即指定那些注解被排除  
	如下是排除了Service注解所标识的类
-->
 	<!-- <context:component-scan base-package="基类包的路径" >
 		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
 	</context:component-scan>
 --> 	
 
 	
 	<!--context:include-filter type="annotation":子节点指定只包含那些表达式的组件,即指定只包含那些注解  ,与use-default-filters搭配使用  ,如下是只向ioc容器中加入Service注解所标识的bean-->
 <!-- 	<context:component-scan base-package="基类包的路径" use-default-filters="false">
 		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
 	</context:component-scan> -->
 	
 	<!-- context:exclude-filter type="assignable":子节点下指定那个类被排除 -->
 	<!-- <context:component-scan base-package="基类包的路径">
 		<context:exclude-filter type="assignable" expression="被排除类的全类名"/>
 	</context:component-scan> -->
 	
 	<!--context:include-filter type="assignable":指定只包含那个类,与use-default-filters搭配使用  -->
 	<!-- <context:component-scan base-package="基类包的路径" use-default-filters="false">
 		<context:include-filter type="assignable" expression="只包括类的全类名"/>
 	</context:component-scan> -->

context:component-sacn元素还会自动注册AutowircedAnnotationBeanPostProcessor实例,该实例可以自动装偶具有@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 的名称作为键值

Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
建议使用 @Autowired 注解

猜你喜欢

转载自blog.csdn.net/qq_43687990/article/details/102594678