Spring容器優先加載由ServletContextListener(對應applicationContext.xml)產生的父容器,而SpringMVC(對應mvc_dispatcher_servlet.xml)產生的是子容器。子容器Controller進行掃描裝配時裝配的@Service注解的實例是沒有經過事務加強處理,即沒有事務處理能力的Service,而父容器進行初始化的Service是保證事務的增強處理能力的。如果不在子容器中將Service exclude掉,此時得到的將是原樣的無事務處理能力的Service。所以應在spring mvc配置文件中,不包括service的掃描。
spring mvc:
<context:component-scan base-package="com.pamirs.pradar.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
spring
<context:component-scan base-package="com.pamirs.pradar.controller"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
添加@transactional主要注意的問題:
<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.
這句話的意思是,<tx:annoation-driven/>只會查找和它在相同的應用上下文件中定義的bean上面的@Transactional注解,如果你把它放在Dispatcher的應用上下文中,它只檢查控制器上的@Transactional注解,而不是你services上的@Transactional注解。
於是,我將事務配置定義在Spring MVC的應用上下文(*-servlet.xml)中,將@Transactional注解打在Controller上,終於事務起作用了。