【SpringMVC】 使用時要注意的地方


  http://jinnianshilongnian.iteye.com/blog/1762632

  http://blog.51cto.com/wenshengzhu/1700340

  http://www.cnblogs.com/kevin-yuan/p/5068448.html

【轉載】https://www.cnblogs.com/haoke/p/4604883.html

在Spring MVC中的配置中一般會遇到這兩個標簽,作為<context:component-scan>的子標簽出現。

存疑:

context:include-filter對於include,  除了掃描base-package包下面的子類,還掃描expression后面的包。
context:exclude-filter對於exclude,即使expression后面的包在base-package下面,也不掃描。

 

但在使用時要注意一下幾點:

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不存在包不精確后都進行掃描的問題。

Spring過濾不需要掃描的包
<context:component-scan base-package="com.alimama"> <!-- 不掃描com.alimama.trace包--> <context:exclude-filter type="regex" expression="com.alimama.trace.*"/> </context:component-scan>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM