https://blog.csdn.net/liuxingsiye/article/details/52171508

通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个表情,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean

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

在注解注入之前也必须在spring的配置文件中做如下配置,我们看下spring.xml文件的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.            http://www.springframework.org/schema/beans/spring-beans.xsd
  7.            http://www.springframework.org/schema/context
  8.            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9. <context:component-scan base-package="com.sparta.trans" use-default-filters="false">
  1. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  1. </context:component-scan>

</beans>

这个配置文件中必须声明xmlns:context 这个xml命名空间,在schemaLocation中需要指定schema:

  1.            http://www.springframework.org/schema/context
  2.            http://www.springframework.org/schema/context/spring-context-3.0.xsd

这个文件中beans根节点下只有一个context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含@Service,@Component,@Repository,@Controller注解修饰的类,use-default-filters属性的默认值为true,这就意味着会扫描指定包下标有@Service,@Component,@Repository,@Controller的注解的全部类,并注册成bean。所以如果仅仅是在配置文件中写<context:component-scan base-package="

  1. com.sparta.trans"/> Use-default-filter此时为true时,那么会对base-package包或者子包下所有的java类进行扫描,并把匹配的java类注册成bean。

所以这用情况下可以发现扫描的粒度还是挺大的,但是如果你只想扫描指定包下面的Controller,那该怎么办?此时子标签<context:incluce-filter>就可以发挥作用了。如下所示

<context:component-scan base-package="com.sparta.trans.controller">

  1. <context:include-filter type="regex" expression="com\.sparta\.trans\.[^.]+(Controller|Service)"/>

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

</context:component-scan>

这样就会只扫描base-package指定下的有@Controller下的java类,并注册成bean

注: context:component-scan节点允许有两个子节点<context:include-filter>和<context:exclude-filter>。filter标签的type和表达式说明如下:

 Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ語法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter

在我们的示例中,将filter的type设置成了正则表达式,regex,注意在正则里面.表示所有字符,而\.才表示真正的.字符。我们的正则表示以Controller或者Service结束的类。

我们也可以使用annotaion来限定,如上面注释掉的所示。这里我们指定的include-filter的type是annotation,expression则是注解类的全名。

但是因为use-dafault-filter在上面并没有指定,默认就为true,所以当把上面的配置改成如下所示的时候,就会产生与你期望相悖的结果(注意base-package包值得变化)

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

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

</context:component-scan>

此时,spring不仅扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类

此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。这样就可以避免在base-packeage配置多个包名来解决这个问题了。

另外在实际项目开发中我们可以发现在base-package指定的包中有的子包是不含有注解的,所以不用扫描,此时可以指定<context:exclude-filter>来进行过滤,说明此包不需要被扫描。所以综上可以看出

use-dafault-filters=”false”的情况下:<context:exclude-filter>可以指定不需要扫描的路径来排除扫描这些文件,<context:include-filter>可以指定需要扫描的路径来进行扫描。但是由于use-dafault-filters的值默认为true,所以这一点在实际使用中还是需要注意一下的。

spring中关于<context:component-scan>的使用说明(转)的更多相关文章

  1. spring中关于<context:component-scan>的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

  2. 关于Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  3. 关于Spring中的<context:annotation-config/>配置(开启注解)

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  4. 关于Spring中的<context:annotation-config/>配置作用

    转自:https://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html 当我们需要使用BeanPostProcessor时,直接在Spri ...

  5. Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  6. spring中的context:include-filter和context:exclude-filter的区别

    在Spring 的配置文件中有: <context:component-scan base-package="njupt.dao,njupt.service"> < ...

  7. spring中注解注入 context:component-scan 的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

  8. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  9. Spring context:component-scan中使用context:include-filter和context:exclude-filter

    Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...

随机推荐

  1. hihocoder1489 Legendary Items 概率期望

    Little Hi is playing a video game. Each time he accomplishes a quest in the game, Little Hi has a ch ...

  2. 使用JQuery提交表单的两种方式选择

    有一个表单,如果使用JQuery提交的话,可以使用下面2中方式,但他们的区别却是根据实际需求需要进行选择的. 第一种:表单按照action路径提交后,页面会刷新. $("#id") ...

  3. CF使用TGP下载后,分卷文件损坏的解决方法

    首先从游戏的列表删除游戏(安装失败出现分卷文件损坏的游戏) 然后进入游戏重新,继续找到该游戏(安装失败的游戏) 点击下载游戏!不会重新下载的,之后下载一些失败的文件,不会花费多少时间,慢慢等待即可 之 ...

  4. 针对IE浏览器的CSS样式(兼容性)

    1. IE hacks: "_"  是IE6 专有的hack; "\9" 对IE6-IE10都有效: "\0"对IE8-IE10都有效: & ...

  5. 一不小心把win10的秘钥卸载了解决方法

    我遇到的第一个问题是Win10家庭版激活失败提示错误代码0xC004C003 然后我百度后看到一个解决方法是卸载秘钥然后再输入秘钥的,于是我执行了slmgr.vbs /upk,发现win10秘钥被卸载 ...

  6. Babelfish 开源通用代码解析服务

    Babelfish 是一个开源的代码解析服务 参考架构 支持的语言 bash go java javascript php ruby c++ typescript 功能 我们可以使用此工具,进行大规模 ...

  7. haproxy prometheus 监控docker-compose 运行试用

    haproxy prometheus 的监控metrics 使用的是exporter ,因为haproxy 对于状态统计报告处理的 比较好,我们可以了stats 同时支持一个csv的api 接口,所以 ...

  8. HDU2220 Eddy's AC难题

    版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/27853287 Eddy's AC难题 Time Limit: 3000/100 ...

  9. C#使用Xamarin开发Android应用程序 -- 系列文章

    Xamarin开发Android应用程序 利用Xamaria构建Android应用-公交发车信息屏 Xamarin版的C# SVG路径解析器 C#使用Xamarin开发可移植移动应用(1.入门与Xam ...

  10. Java 8后的首个长期支持版本Java 11

    Java 11是Java8后的首个长期支持版本.按照 Oracle 公布的支持路线图,Java 11 将会获得 Oracle 提供的长期支持服务,直至2026年9月. 按照官方的说法,新的发布周期会严 ...