• 关于 spring MVC 配置自动扫描中 use-default-filters 属性


    1、springMVC 设置扫描 Bean 的两种常见写法

         1.1、先看第一种常见的配置:默认

    <!-- 配置Controller扫描 -->
    <context:component-scan base-package="com.jeenotes.ssm" />

        1.2、第二种,自定义,只扫描 control

    <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
    <context:component-scan base-package="com.jeenotes.ssm" 
            use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

     

    2、use-default-filters="false"

        1.1 中默认情况下,该属性是 true ,作用就是:是否自动扫描带有 @Component、@Repository、@Service 和 @Controller 的类;
     
        1.2 中如果把该属性设置成 false,那么就可以自定义过滤要扫描的 Bean
     
    //这种写法是:去扫描某某某注解的 bean
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     
    //这种写法是:不去扫描某某注解的 bean
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

     

    3、补充:添加 && 排除      

         如果添加和排除的是相同,则必须 include-filter 在前,exclude-filter 在后,否则配置不符合 spring -context-3.0.xsd 要求,Spring容器解析时会出错。
     
     上面的配置会把@Repository注解的类排除掉。
     
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        
        博客地址:http://www.cnblogs.com/niceyoo
     
  • 相关阅读:
    DS博客作业04--图
    DS博客作业03--树
    DS博客作业02--栈和队列
    DS博客作业01--线性表
    C博客作业05--指针
    C语言博客作业04--数组
    C博客作业03--函数
    博客作业——循环结构
    C博客作业05-指针
    C博客作业04--数组
  • 原文地址:https://www.cnblogs.com/niceyoo/p/9281564.html
Copyright © 2020-2023  润新知