在Spring MVC中的配置中一般會遇到這兩個(gè)標(biāo)簽,作為<context:component-scan>的子標(biāo)簽出現(xiàn)。
但在使用時(shí)要注意一下幾點(diǎn):
1.在很多配置中一般都會吧Spring-common.xml和Spring-MVC.xml進(jìn)行分開配置,這種配置就行各施其職一樣,顯得特別清晰。
在Spring-MVC.xml中只對@Controller進(jìn)行掃描就可,作為一個(gè)控制器,其他的事情不做。
在Spring-common.xml中只對一些事務(wù)邏輯的注解掃描。
2.現(xiàn)在給定一個(gè)項(xiàng)目包的機(jī)構(gòu):
com.fq.controlller
com.fq.service
就先給定這兩個(gè)包機(jī)構(gòu)
(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。哈哈哈,這點(diǎn)需要注意。他一般會導(dǎo)致一個(gè)常見的錯(cuò)誤,那就是事務(wù)不起作用,補(bǔ)救的方法是添加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不存在包不精確后都進(jìn)行掃描的問題。
posted @
2015-10-29 10:25 kelly 閱讀(255) |
評論 (0) |
編輯 收藏