[Spring实战系列](14)Bean的自动检测

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/50650102 即使有助于完全消除Spring注解中的和元素,但是还是不能完全消除,仍然需要使用元素显示定义Bean。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/50650102
即使 <context:annotation-config>有助于完全消除Spring注解中的<property>和<constructor-arg>元素,但是还是不能完全消除,仍然需要使用<bean>元素显示定义Bean。因此 <context:component-scan>元素出现了,它除了 完成<context:annotation-config>一样的工作,还允许Spring自动检测Bean定义Bean。这就意味着我们不使用<bean>元素,Spring应用中的大多数(或者所有)Bean都能够实现定义和装配。

为了配置Spring自动检测,需要使用<context:component-scan>元素来代替<context:annotation-config>元素:
 
   
<?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">
<context:component-scan base-package="">
</context:component-scan>
 
</beans>

<context:component-scan>元素会 扫描指定的包以及所有子包,并 查找出能够自动注册为Spring Bean的类base-package属性标示了 <context:component-scan>元素所扫描的包

为自动检测标注Bean

默认情况下, <context:component-scan>查找使用构造型(stereotype)注解所标注的类,这些特殊的注解如下:
类型 说明
@component 通用的构造型注解,标示该类为Spring 组件。
@Controller
标识将该类定义为Spring MVC controller。
@Repository
标识将该类定义为数据仓库(例如:Dao层)。
@Service 标识将该类定义为服务(例如:Service层)。

1. @component

假设我们应用上下文中仅仅包含Student和School两个Bean。我们可以配置 <context:component-scan>元素并使用@Component注解标注Student和School类,从而消除显示的<bean>定义。
 
   
package com.sjf.bean;
 
import org.springframework.stereotype.Component;
 
/**
* 学校实体类
* @author sjf0115
*
*/
@Component
public class School {
private String name;
private String location;
...
}
Spring 扫描com.sjf.bean包时,会发现 使用Component注解所标注的School,并 自动将它注册为Spring BeanBean的ID默认为无限定类名(第一个字母小写),School Bean的ID为school

下面我们标注Student类:
 
   
package com.sjf.bean;
 
import org.springframework.stereotype.Component;
 
/**
* 学生实体类
* @author sjf0115
*
*/
@Component("studentBean")
public class Student {
private String name;
private int age;
private School school;
...
}

这种场景下,我们 指定了一个Bean ID作为@Component注解的参数。该Bean 的ID不会使用默认的类名,而是显示的命名为studentBean。

当使用<context:component-scan>时,基于注解的自动检测只是一种扫描策略。下面让我们来了解其他的扫描策略来查找候选Bean。

2. 过滤组件扫描

在如何扫描来获得候选Bean方面, <context:component-scan>元素非常灵活。通过为 <context:component-scan>配置<context:include-filter>和<context:exclude-filter>子元素,我们可以随意调整扫描行为。

假设我们基于注解让<context:component-scan>自动注册所有实现某个接口的类,我们不得不浏览每一个接口实现的类,并使用@Component来标注它们,非常不方便。所以我们替换掉基于注解的组件扫描策略,再增加一个包含过滤器来要求<context:component-scan>注册以及排除类。
 
   
<context:component-scan base-package="com.sjf.bean">
<context:include-filter type="" expression=""/>
<context:exclude-filter type="" expression=""/>
</context:component-scan>

<context:include-filter>的type和expression属性一起协作来定义组件扫描策略。我们还可以选择如下任意一种过滤器:
类型 说明
annotation 过滤器扫描使用指定注解标注的那些类,通过expression属性指定要扫描的注解
assignable 过滤器扫描派生于expression属性所指定类型的那些类
aspectj 过滤器扫描与expression属性所指定的AspectJ表达式所匹配的那些类
custom 使用自定义的org.springframework.core.type.TypeFilter实现类,该类由expression属性指定。
regex 过滤器扫描类的名称与expression属性所指定的正则表达式所匹配的那些类

除了使用<context:include-filter>告知<context:component-scan>哪些类需要注册为Spring Bean以外,我们还可以使用 <context:exclude-filter>来告知 <context:component-scan>哪些类不需要注册为Spring Bean。

我们实现了两个接口,一个是Worker接口(员工),一个是Performer(表演者)。然后根据这两个接口,分别实现了接口的实现类:
 
   
package com.sjf.bean;
/**
* 农民实体类
* @author sjf0115
*
*/
public class Farmer implements Worker {
 
public void work() {
System.out.println("正在辛勤的耕地...");
}
}

我们使用<context:include-filter>告知<context:component-scan>实现了Worker接口的实现类需要注册为Spring Bean,使用 <context:exclude-filter>来告知实现了Perfomer接口的实现类 不需要注册为Spring Bean。
 
   
<context:component-scan base-package="com.sjf.bean">
<context:include-filter type="assignable" expression="com.sjf.bean.Worker"/>
<context:exclude-filter type="assignable" expression="com.sjf.bean.Performer"/>
</context:component-scan>

我们测试一下上述过滤器是否起作用了:
 
   
Farmer farmer = (Farmer) context.getBean("farmer");
farmer.work();

运行结果:

正在辛勤的耕地...  

 
   
Singer singer = (Singer) context.getBean("singer");
singer.perform();

运行结果:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'singer' is defined  



参考:《Spring实战》




目录
相关文章
|
25天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
32 1
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
1月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
61 0
|
28天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
66 1
|
28天前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
66 0
|
1天前
|
canal 缓存 关系型数据库
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
|
1天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
9天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
18 1
|
14天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
19 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
|
14天前
|
Java 数据库 Spring
切面编程的艺术:Spring动态代理解析与实战
切面编程的艺术:Spring动态代理解析与实战
26 0
切面编程的艺术:Spring动态代理解析与实战