Spring与SpringMVC的容器冲突解决

博客园

1.在applicationContext.xml中配置:

1
2
3
4
5
<!-- Spring容器中注册非@controller注解的Bean -->
<context:component-scan base-package="com.hafiz.www">
<!--排除@Controller-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

2.applicationContext-MVC.xml中配置
1
2
3
4
5
6
7
<!-- SpringMVC容器中只注册带有@controller注解的Bean -->
<!--use-default-filters:默认true 会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描
这里改为false让其只扫描@Controller注解-->
<context:component-scan base-package="com.hafiz.www" use-default-filters="false">
<!--引入@Controller-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>