还有一个请高手说下自己的理解:
<context:component-scan base-pack="com.test">
<context:include-filter type="annotation" expression="org.springframework.mvc.Controller"/>
</context:component-scan>
我的理解是:自动扫描包com.test或者子包下面含有@Component,@Controller,@Respotory,@Service这样注解的类,并注册进容器bean中
filter是扫描含有@Controller注解的类,然后注册到bean容器中。(但是上面不是package扫描了么,要这个filter有什么用?多此一举么)求高手解释

解决方案 »

  1.   

    The following example shows the XML configuration ignoring all @Repository annotations and
    using "stub" repositories instead.
    <beans>
    <context:component-scan base-package="org.example">
    <context:include-filter type="regex" expression=".*Stub.*Repository"/>
    <context:exclude-filter type="annotation"
    expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
    </beans>可以看看官方说明。
      

  2.   

    include filter估计是只用它include到的,exclude恰好相反
      

  3.   

    exclude这个我知道: exclude是和include相反但是我的应用当真如果有其他注解@Component,@Controller,@Respotory,@Service类,那么是谁扫描的就是说,你加了filter之后,base-pack里面的其他注解类是怎么处理的?
      

  4.   


    我看的是Spring in Action 3rd。可能是我英语不好把,感觉还没有区分加filter和不加filter的区别。不加了,我知道<context:component-scan>这个是根据base-pack扫描,然后注册到容器中加了filter他是什么情况,如果仅仅扫过滤的,那么其他的需要注入的bean呢?如果这种情况下其他的注解class也被扫,那么还要include-filter做什么。
      

  5.   

    用了include filter之后,其他的就等于过滤了,不扫描了,只扫描include标明的
      

  6.   

    嗯。我可能陷入误区了公司的项目中写的是这样:
    <context:annotation-config/>
    <context:component-scan base-package="**.**.**" >
    <context:execute-filter type="annotation" expression="org.springframework.stereotype.Controller"
    </context:conmponet-scan>
      

  7.   

    这是过滤type=stereotype的注解。
      

  8.   

    是的。但是我要扫描某个package或者子pack下面的所以有注解的class,怎么办,你这里只过滤这种的,如果我用其他的stereotype类型的注解呢?
      

  9.   

    http://blog.sina.com.cn/s/blog_57769b7b0100tt5x.html楼主去看下 我也在看
      

  10.   

    我想问一下,那个context:exclude-filter type="regex" 中type后面的值是什么意思、
      

  11.   


    context:include-filter对于include,我的理解是,除了扫描base-package包下面的子类,还扫描expression后面的包。
    context:exclude-filter对于exclude,我认为,即使expression后面的包在base-package下面,也不扫描。是这样吗?
      

  12.   


    context:include-filter对于include,我的理解是,除了扫描base-package包下面的子类,还扫描expression后面的包。
    context:exclude-filter对于exclude,我认为,即使expression后面的包在base-package下面,也不扫描。是这样吗?
    是的,因为你在base-package定义了一个包所在的路径,可是如果你需要扫描其它的类或者啥的,可是又不在你base-package包里面,那么就需要 include-filter
    相反的,exclude就是去除你不需要扫描的class。比如@Controller注释的,因为这个需要在SpringMVC的配置文件配置的