首先来看下spring -context-3.0.xsd中关于<contenxt:component-scan/>属性use-default-filters的定义:
<xsd:attribute name="use-default-filters" type="xsd:boolean" default="true"> <xsd:annotation> <xsd:documentation><![CDATA[ Indicates whether automatic detection of classes annotated with @Component, @Repository, @Service, or @Controller should be enabled. Default is "true". ]]> </xsd:documentation> </xsd:annotation> </xsd:attribute>
即它用来指示是否自动扫描带有@Component、@Repository、@Service和@Controller的类。默认为true,即默认扫描。如果想要过滤其中这四个注解中的一个,比如@Repository,可以添加如下子标签:
<context:component-scan base-package="tv.crm" scoped-proxy="targetClass" use-default-filters="true"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
而<context:include-filter/>子标签是用来添加扫描注解的。
但是如果添加和排除的是相同,比如:
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
则必须include-filter在前,exclude-filter在后,否则配置不符合spring -context-3.0.xsd要求,Spring容器解析时会出错。上面的配置会把@Repository注解的类排除掉。
相关推荐
使用<context:component-scan/>标签需要在 Spring 配置文件中添加以下代码: ```xml <context:component-scan base-package="com.example"/> ``` 其中,base-package 指定了要扫描的包名。 Spring 框架会自动检测该...
<context:component-scan base-package="com.mvc.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component...
当在Spring配置文件中加入`<context:component-scan base-package="leot.test"/>`,Spring会扫描指定包(本例中为"leot.test")及其子包下的所有类,查找带有上述注解的类,并将其注册为Spring管理的Bean。...
<context:component-scan base-package="com.dn" use-default-filters="false"> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan>...
例如:<context:component-scan base-package="tv.huan.weisp.web.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> ...
<context:component-scan base-package="org.bc.redis.controller" use-default-filters="true"> </context:component-scan> ``` 总结 在使用 Spring 和 SpringMVC 配置事务管理时,需要正确配置 Spring 和 ...
示例:<context:component-scan base-package="com.web.annotation" resource-pattern="repository/*.class"use-default-filters="true"> 在<context:component-scan/>元素中,可以使用<context:include-filter>和...
首先,我们需要理解Spring的`<context:component-scan>`元素的作用。这个元素用于开启对指定包及其子包内使用了Spring注解的类的扫描。在这个例子中,`base-package`属性指定了基础包`com.cjw.test.controller`,`...
<context:component-scan base-package="com.sishuok.es" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.ControllerAdvice"/> </context:...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................