spring中关于<context:component-scan>的使用说明
来源:互联网 发布:mysql用户密码修改 编辑:程序博客网 时间:2024/06/05 19:34
注:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用在xml中再配置了,因为前者包含了后者。另外<context:annotation-config/>还提供了两个子标签 <context:include-filter>和 <context:exclude-filter>
在注解注入之前也必须在spring的配置文件中做如下配置,我们看下spring.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.sparta.trans" use-default-filters="false"></beans><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
这个配置文件中必须声明xmlns:context 这个xml命名空间,在schemaLocation中需要指定schema:
http://www.springframework.org/schema/context 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="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">
<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 TypeExamples ExpressionDescriptionannotationorg.example.SomeAnnotation符合SomeAnnoation的target classassignableorg.example.SomeClass指定class或interface的全名aspectjorg.example..*Service+AspectJ語法regexorg\.example\.Default.*Regelar Expressioncustomorg.example.MyTypeFilterSpring3新增自訂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>的使用说明
- spring中注解注入<context:component-scan>的使用说明
- Spring配置<context:component-scan>的使用说明
- spring的<context:component-scan >使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring<context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明。
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- java 入门学习笔记(封装,继承,多态)
- JS高级之运动篇
- google vr sdk(全景图的播放)
- Spring@Autowired注解与自动装配
- Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
- spring中关于<context:component-scan>的使用说明
- Ehcache与redis比较
- Java HashMap的死循环
- Java序列化与反序列化
- 大数据系列修炼-Scala课程09
- Spring注解@Component、@Repository、@Service、@Controller区别
- Json
- test
- 声明式事物管理