`
wusuoya
  • 浏览: 637670 次
  • 性别: Icon_minigender_2
  • 来自: 成都
社区版块
存档分类
最新评论

context:component-scan扫描使用上的容易忽略的use-default-filters

    博客分类:
  • SSH
 
阅读更多
问题
如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean。正确

Java代码  收藏代码
<context:component-scan base-package="org.bdp.system.test.controller">  
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan> 
 
但是如下方式,不仅仅扫描@Controller,还扫描@Service/@Repository的Bean,可能造成一些问题

Java代码  收藏代码
<context:component-scan base-package="org.bdp">  
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan> 

这个尤其在springmvc+spring+hibernate等集成时最容易出问题的地,最典型的错误就是:
事务不起作用

这是什么问题呢?
分析
1、<context:component-scan>会交给org.springframework.context.config.ContextNamespaceHandler处理;

Java代码  收藏代码
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser()); 

2、ComponentScanBeanDefinitionParser会读取配置文件信息并组装成org.springframework.context.annotation.ClassPathBeanDefinitionScanner进行处理;
3、如果没有配置<context:component-scan>的use-default-filters属性,则默认为true,在创建ClassPathBeanDefinitionScanner时会根据use-default-filters是否为true来调用如下代码:

Java代码  收藏代码
  protected void registerDefaultFilters() { 
this.includeFilters.add(new AnnotationTypeFilter(Component.class)); 
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); 
try { 
    this.includeFilters.add(new AnnotationTypeFilter( 
            ((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false)); 
    logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning"); 

catch (ClassNotFoundException ex) { 
    // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip. 

try { 
    this.includeFilters.add(new AnnotationTypeFilter( 
            ((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false)); 
    logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning"); 

catch (ClassNotFoundException ex) { 
    // JSR-330 API not available - simply skip. 



可以看到默认ClassPathBeanDefinitionScanner会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描。如果细心,到此我们就找到问题根源了。


4、在进行扫描时会通过include-filter/exclude-filter来判断你的Bean类是否是合法的:

Java代码  收藏代码
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException { 
    for (TypeFilter tf : this.excludeFilters) { 
        if (tf.match(metadataReader, this.metadataReaderFactory)) { 
            return false; 
        } 
    } 
    for (TypeFilter tf : this.includeFilters) { 
        if (tf.match(metadataReader, this.metadataReaderFactory)) { 
            AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); 
            if (!metadata.isAnnotated(Profile.class.getName())) { 
                return true; 
            } 
            AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class); 
            return this.environment.acceptsProfiles(profile.getStringArray("value")); 
        } 
    } 
    return false; 



首先通过exclude-filter 进行黑名单过滤;
然后通过include-filter 进行白名单过滤;
否则默认排除。

结论
Java代码  收藏代码
<context:component-scan base-package="org.bdp">  
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan> 

为什么这段代码不仅仅扫描@Controller注解的Bean,而且还扫描了@Component的子注解@Service、@Reposity。因为use-default-filters默认为true。所以如果不需要默认的,则use-default-filters=“false”禁用掉。


请参考
《SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结》
《第三章 DispatcherServlet详解 ——跟开涛学SpringMVC》中的ContextLoaderListener初始化的上下文和DispatcherServlet初始化的上下文关系。

如果在springmvc配置文件,不使用cn.javass.demo.web.controller前缀,而是使用cn.javass.demo,则service、dao层的bean可能也重新加载了,但事务的AOP代理没有配置在springmvc配置文件中,从而造成新加载的bean覆盖了老的bean,造成事务失效。只要使用use-default-filters=“false”禁用掉默认的行为就可以了。

问题不难,spring使用上的问题。总结一下方便再遇到类似问题的朋友参考。
分享到:
评论

相关推荐

    spring组件扫描contextcomponent-scan使用详解.pdf

    &lt;context:component-scan/&gt;标签提供了 use-default-filters 属性,该属性控制着是否使用默认的过滤器。默认情况下,use-default-filters 属性为 true,即使用默认的过滤器。如果将其设置为 false,则需要手动配置...

    spring mvc

    &lt;context:component-scan base-package="leot.test" use-default-filters="false"&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/&gt; &lt;context:include-filter ...

    集成springmvc spring hibernate的配置

    &lt;context:component-scan base-package="com.mvc.web" use-default-filters="false"&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component...

    SpringMVC和Spring的配置文件扫描包详解

    &lt;context:component-scan&gt; 标签有一个 use-default-filters 属性,该属性默认为 true,这意味着会扫描指定包下的全部的标有 @Component 的类,并注册成 Bean。因此,如果仅仅是在配置文件中这么写 &lt;context:...

    Spring+SpringMVC配置事务管理无效原因及解决办法详解

    &lt;context:component-scan base-package="org.bc.redis.service" use-default-filters="true"&gt; &lt;/context:component-scan&gt; ``` SpringMVC 的配置文件 springmvc.xml: ``` &lt;context:component-scan base-package="org...

    Java之Spring注解配置bean实例代码解析

    在使用&lt;context:component-scan/&gt;元素时,可以使用base-package属性指定需要扫描的基类包,resource-pattern属性可以用来过滤特定的类。如果需要扫描多个包,可以使用逗号分隔。 示例:&lt;context:component-scan ...

    基于注解的Spring_3.0.x_MVC

    &lt;context:component-scan base-package="com.dn" use-default-filters="false"&gt; &lt;context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/&gt; &lt;/context:component-scan&gt;...

    Spring MVC 注解自动扫描失效原因分析

    在Spring MVC框架中,注解自动扫描是核心特性之一,它允许开发者通过在类或方法上使用特定注解(如@Controller、@Service、@Repository等)来声明组件,然后由Spring容器自动检测并管理这些组件。然而,在实际开发中...

    java基于spring注解AOP的异常处理的方法

    &lt;context:component-scan base-package="com.sishuok.es" use-default-filters="false"&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.ControllerAdvice"/&gt; &lt;/context:...

    spring-framework-reference-4.1.2

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

Global site tag (gtag.js) - Google Analytics