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

即使 <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实战》




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值