您的位置:首页 > 编程语言 > Java开发

springmvc-servlet.xml中use-default-filters的作用

2016-02-15 12:34 453 查看
1、<!-- 启用注解扫描,并定义组件查找规则 ,mvc层只负责扫描@Controller -->

<context:component-scan base-package="com.nn.web.controller"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>


2、在context:component-scan可以添加use-default-filters,spring配置中的use-default-filters用来指示是否自动扫描带有@Component、@Repository、@Service和@Controller的类。默认为true,即默认扫描。

3、如果想要过滤其中这四个注解中的一个,比如@Repository,可以添加<context:exclude-filter
/>子标签:

<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>


</pre><p></p><pre>


4、而<context:include-filter/>子标签是用来添加扫描注解的。

<context:component-scan base-package="tv.crm" scoped-proxy="targetClass" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>


5、但是如果添加和排除的是相同,则必须include-filter在前,exclude-filter在后,否则配置不符合spring -context-3.0.xsd要求,Spring容器解析时会出错。上面的配置会把@Repository注解的类排除掉。

<span style="font-size:18px;"><context:component-scan base-package="tv.crm" scoped-proxy="targetClass" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: