关于 spring 和 springMVC 扫描包时,springMVC只扫描 @Controller 的问题
最近在搭建 ssm 过程的时候发现,在spring和springmvc中都配置了扫描包的注解(<context:component-scan base-package=“com.ithm” >),springmvc 只配置 @Controller ,spring 配置其他的,那么现在问题来了,为什么他俩就不能一起配置呢?
下面是 spring 和 springmvc 的配置
applicationContext.xml
<context:component-scan base-package="com.kz" >
<!-- 配置哪些注解不扫描 expression 需要放全路径 -->
<!-- 不扫描 Controller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/》
</context:component-scan>
mybatis.xml
<context:component-scan base-package="com.kz">
<!-- 只扫描 Controller -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>–>
</context:component-scan>
因为如果让SpringMVC扫描@Service或者其他对数据库操作的对象的有关注解的话,会让 SpringCore失去对事务的控制,所以最好不要那样写。虽然直接在 spingmvc中配置扫描全部可行