Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service
exclude掉,此时得到的将是原样的无事务处理能力的Service。
改正:
1、applicationContext.xml:
<context:component-scan base-package="com.will"/>
dispatcher.xml:
<context:component-scan base-package="com.will" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /></context:component-scan>
2、只用dispatcher.xml