当前位置: 代码迷 >> Java Web开发 >> Spring自动扫描 <context:componet-scan>和加了<context:include-filter>以后有什么区别
  详细解决方案

Spring自动扫描 <context:componet-scan>和加了<context:include-filter>以后有什么区别

热度:4794   发布时间:2013-02-25 21:05:57.0
Spring自动扫描 <context:componet-scan>和加了<context:include-filter>之后有什么区别
我先说下自己关于这个自动扫描的理解:
1,<context:componet-scan base-pack="*.*"></context:componet-scan>
在xml配置了这个后,spring可以自动去扫描base-pack下面的或者子包下面的class,扫描如果有
@Component,@Controller,@Respotory,@Service这些注解的类,然后注册为java bean容器中的bean。这个很好理解
2,然后1中的子标签(这个我理解的有点混)
这里面有几个属性 type,expression
eg:<context:include-filter type="assignable" expression="com.test.UserDao"/>
然后开始过滤类型匹配为UserDao的。就是说实现UserDao的类都注册为容器下的bean

我看Spring in Aciton中说加了这个filter之后,貌似不用再去管@Component,@Controller,@Respotory,@Service有这些注解的类了。(这样理解对不)

还有一个1中的 use-default-filters="false/true"的这个属性
如果为false,那么扫描只用到了filter了。就是只识别注册filter里面的。
那么我想问,那我其他加了@Component,@Controller,@Respotory,@Service这些注解的类怎么组成成bean呢?


备注:不知道我的问题说清楚 了没。
或者根本还没理解这个自动扫描标签。



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>
可以看看官方说明。
引用:
还有一个请高手说下自己的理解:
<context:component-scan base-pack="com.test">
<context:include-filter type="annotation" expression="org.springframework.mvc.Controller"/>
</context:component-scan>
我的理解是:自动扫描包com.t……

include filter估计是只用它include到的,exclude恰好相反
引用:
引用:
引用:还有一个请高手说下自己的理解:
<context:component-scan base-pack="com.test">
<context:include-filter type="annotation" expression="org.springframework.mvc.Controller"/……

用了include filter之后,其他的就等于过滤了,不扫描了,只扫描include标明的
引用:
引用:引用:引用:
引用:还有一个请高手说下自己的理解:
<context:component-scan base-pack="com.test">
<context:include-filter type="ann……
这是过滤type=stereotype的注解。
  相关解决方案