在Spring MVC中的配置中通常會遇到這兩個標籤,做爲<context:component-scan>的子標籤出現。spring
但在使用時要注意一下幾點:express
1.在不少配置中通常都會吧Spring-common.xml和Spring-MVC.xml進行分開配置,這種配置就行各施其職同樣,顯得特別清晰。spa
在Spring-MVC.xml中只對@Controller進行掃描就可,做爲一個控制器,其餘的事情不作。code
在Spring-common.xml中只對一些事務邏輯的註解掃描。component
2.如今給定一個項目包的機構:xml
com.fq.controlllerblog
com.fq.service事務
就先給定這兩個包機構io
(1)在Spring-MVC.xml中有如下配置:class
<!-- 掃描@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不存在包不精確後都進行掃描的問題。