因爲Spring 是父容器, Spring MVC是子容器, 子容器可以訪問父容器的bean,父容器不能訪問子容器的bean,所以往往項目中springmvc只掃描控制層controller,spring和springmvc的配置文件分開來配置。
1、在Spring 的配置文件中有:
context:include-filter標籤:添加對某個註解的掃描
<context:component-scan base-package="*.Controller">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
默認use-default-filters="true"所以會自動對 @Component、@ManagedBeuse-default-filters=“true”、@Named註解的Bean進行掃描,context:exclude-filter不對Controller的註解進行掃描。
2、在SpringMVC的配置文件中有:
context:exclude-filter標籤:排除對某個註解的掃描
<context:component-scan base-package="*.Controller"
use-default-filters="false">
<context:include-filter
expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
使用use-default-filters="false"不會對 @Component、@ManagedBeuse-default-filte