`

context:component-scan

阅读更多

1. 扫描过滤方式 
过滤器类型 说明 
注释 假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。 
类名指定 通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。 
正则
表达式 通过正则表达式定义过滤的类,如下所示: com.baobaotao.Default.* 
AspectJ 表达式 通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+ 

下面是一个简 单的例子:

Java代码
  1. <context:component-scan base-package="com.baobaotao">     
  2.     <context:include-filter type="regex"       
  3.         expression="com.baobaotao.service..*"/>     
  4.     <context:exclude-filter type="aspectj"       
  5.         expression="com.baobaotao.util..*"/>     
  6. </context:component-scan>    


值得注意的是 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。 

默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标,如以下代码所示: 


清单 24. 通过 @Scope 指定 Bean 的作用范围

Java代码
  1. package com.baobaotao;      
  2. import org.springframework.context.annotation.Scope;      
  3. …      
  4. @Scope("prototype")      
  5. @Component("boss")      
  6. public class Boss {      
  7.     …      
  8. }     


这样,当从 Spring 容器中获取 boss Bean 时,每次返回的都是新的实例了。 
采用具有特殊语义的注释 

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 
应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。 


注释配置和 XML 配置的适用场合 

是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因: 

注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。 
如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。 
注释配置 往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于 @Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。 
所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。 

 

 

 

 

 

 

 

 

Spring applicationContext.xml的<context:component-scan>標籤用途比我想像的還要實用。而且後來才知道,有了<context:component-scan>,另一個<context:annotation-config/>標籤根本可以移除掉,因為被包含進去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下尋找有@Component和@Configuration的target Class。而現在如下的飯粒:

 

<context:component-scan base-package="com.foo" use-default-filters="false">
    <context:include-filter type="regex" expression="com.foo.bar.*Config"/>
    <context:include-filter type="regex" expression="com.foo.config.*"/>
</context:component-scan>

  <context:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。

  filter標籤在Spring3有五個type,如下:

 

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ語法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter

  所以上例用的regex就有個語病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因為小數點在Regex是任意字元,是故要用\.把小數點跳脫為佳。(2010/3/15補充:但要使用\.方式,其use-default-filters不能為false,否則抓不到,感覺是Bug)

  Spring3提供豐富的Filter支援,有益配置策略,不需面臨Configuration Hell,比如Regex的com\.foo\.*\.action\.*Config,這樣就可以找到com.foo package下所有action子package的*Config的target class。

  2010/3/18補充:後來在AppConfig前忘了加@Component,AppConfig內尚留有@Bean,奇怪的是還是能work。我猜有加@Bean的method的class,若沒特別註解AppConfig是@Repository、@Service還是@Configuration,一律被Spring3視為@Component。

分享到:
评论
2 楼 ttaale 2011-03-14  
ted_yuen 写道
请问一下:

eclipse项目里这么配置每问题。部署出来以后,就说找不到BEAN。
我的不是WEB 项目。怎么办。。


如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释
注入bean:
        @Inject
private WebService  webService;

如果一个接口有多个实现要 为每个实现指定bean 名, @Service("webService1")
注入bean
@Inject
@Named("webService1")
private WebService  webService;

1 楼 ted_yuen 2011-03-10  
请问一下:

eclipse项目里这么配置每问题。部署出来以后,就说找不到BEAN。
我的不是WEB 项目。怎么办。。

相关推荐

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下

    Spring扫描器—spring组件扫描使用详解

    NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898

    Spring注释 注入方式源码示例,Annotation

    &lt;context:component-scan base-package="Mode"&gt;&lt;/context:component-scan&gt; //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; ...

    springMVC技术概述

    配置使用注解的Handler和Service等等使用&lt;context:component-scan&gt; 不过springBoot已经省略了这些配置 常用注解:@Controller @RestController(Controller+ResponseBody) @Service @Transactional @Mapper @...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:component-scan base-package="org.whvcse"&gt;&lt;/context:component-scan&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; &lt;aop:pointcut id="defaultServiceOperation" ...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 ... xmlns:xsi=... &lt;context:component-scan base-package="com.haijian" /&gt;

    springmvcwendang

    &lt;context:component-scan base-package="com.createnets.springmvc.web" /&gt; (2)新建一个Student类 用于测试注解 (3)配置注解 @Controller @RequestMapping("/list") (3)配置视图名称 &lt;!-- 配置视图名称 -...

    一个整合ssm框架的实例

    &lt;context:component-scan base-package="com.jxy.java.service" /&gt; &lt;!-- 导入数据库配置文件 --&gt; &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt; &lt;!-- 配置数据库连接池 --&gt; ...

    Spring AOP配置源码

    &lt;context:component-scan base-package="com.spring.*"/&gt; &lt;aop:config&gt; &lt;aop:aspect id="aspect" ref="logIntercepter"&gt; &lt;aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/&gt; ...

    quartz 定时任务

    &lt;context:component-scan base-package="cn.ly.quartz.service" /&gt; &lt;!-- job --&gt; class="org.springframework.scheduling.quartz.JobDetailFactoryBean"&gt; &lt;!-- trigger触发器 --&gt; ...

    维生药业小项目 SSH简单学习项目

    维生药业小项目 SSH简单学习项目 &lt;?xml version="1.0" encoding="UTF-8"?&gt; ... xmlns:context="http://www.springframework.org/schema/context" ... &lt;context:component-scan base-package="com.sixth" /&gt; &lt;/beans&gt;

    Spring Context测试

    学习spring组件扫描(Component Scanning)的代码 使用方法:直接把工程导入,直接Run ...&lt;context:component-scan base-package="com.test"&gt;&lt;/context:component-scan&gt; 2.在需要装配的类的上面全部加上@Component

    spring杂谈 作者zhang KaiTao

    1.26 context:component-scan扫描使用上的容易忽略的use-default-filters 1.27 idea内嵌jetty运行springmvc项目报ConversionFailedException 1.28 springmvc 3.2 @MatrixVariable注解 1.29 spring3.2 带matrix变量的...

    spring

    context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml配置文件中的bean元素,简化配置 context:component-scan是上下文:annotation-config的超集,配置了前者则不需要配置...

    java微信公众号MVC开发框架

    spring配置文件中唯一需要配置的bean是WeixinConfigurer类,&lt;context:component-scan base-package="com.github.jweixin.jwx.weixin.service" /&gt;是可选配置,但里面封装了微信接口服务类,建议一定要配置进spring...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;aop:config proxy-...

    Maven拆分代码.zip

    &lt;context:component-scan base-package="com.itheima.service"/&gt; &lt;!--aop面向切面编程,切面就是切入点和通知的组合--&gt; &lt;!--配置事务管理器--&gt; &lt;!--配置事务的通知--&gt; &lt;tx:advice id="advice"&gt; &lt;tx:...

    springweb3.0MVC注解(附实例)

    &lt;context:component-scan base-package="com.baobaotao.web"/&gt; &lt;!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; AnnotationMethodHandlerAdapter"/&gt; &lt;!-- ③:对模型视图名称的解析,即在...

Global site tag (gtag.js) - Google Analytics