码迷,mamicode.com
首页 > 编程语言 > 详细

【SpringMVC】<context:include-filter>和<context:exclude-filter>

时间:2020-04-07 18:29:56      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:div   blog   package   一个   html   style   出现   framework   contex   

在Spring MVC中的配置中一般会遇到这两个标签,作为<context:component-scan>的子标签出现。

但在使用时要注意一下几点:

1.在很多配置中一般都会吧Spring-common.xml和Spring-MVC.xml进行分开配置,这种配置就行各施其职一样,显得特别清晰。

在Spring-MVC.xml中只对@Controller进行扫描就可,作为一个控制器,其他的事情不做。

在Spring-common.xml中只对一些事务逻辑的注解扫描。

2.现在给定一个项目包的机构:

com.fq.controlller

com.fq.service

就先给定这两个包机构

(1)在Spring-MVC.xml中有以下配置:

 

 

 

<!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。

(2)在Spring-common.xml中有如下配置:

<!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。

转自 https://www.cnblogs.com/haoke/p/4604883.html

【SpringMVC】<context:include-filter>和<context:exclude-filter>

标签:div   blog   package   一个   html   style   出现   framework   contex   

原文地址:https://www.cnblogs.com/xd502djj/p/12654884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!