<context:include-filter>

  在xml配置<context:component-scan>,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果有@Component @Controller@Service等这些注解的类,则把这些类注册为bean

注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置,因为前者包含了后者。另外<context:component-scan>还提供了两个子标签

.        <context:include-filter>

.       <context:exclude-filter>

   其中,<context:component-scan>有一个use-default-filters属性,改属性默认为true,意味着会扫描指定包下的全部的标有@Component的类,并注册成bean

     <context:component-scan base-package="com.smart">  

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

    </context:component-scan>  

   此时,spring不仅扫描@Controller,还扫描指定包所在的子包service包下注解@Service的java类,此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以

   <context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描

       context:include-filter过滤器有五种type:

  assignable-指定扫描某个接口派生出来的类
  annotation-指定扫描使用某个注解的类
  aspectj-指定扫描AspectJ表达式相匹配的类
  custom-指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类
  regex-指定扫描符合正则表达式的类

 <context:component-scan base-package="com.smart">
        <!-- 配置自己设定的过滤条件 regex包过滤 所有匹配下面的包路径里面的类全部被spring实例化 -->
        <context:include-filter type="regex" expression=".*.common.*"/>
        <context:include-filter type="regex" expression=".*.aop.*"/>
        <!-- 配置自己设定的过滤条件 aspectj类名称过滤 所有继承和扩展自Base的类都被实例化 -->
        <context:include-filter type="aspectj" expression="com.smart.base+"/>
 </context:component-scan>

 

  


  

posted on 2018-12-22 20:51  溪水静幽  阅读(3328)  评论(0)    收藏  举报