spring-context.xml
spring-mvc.xml
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
spring父容器不掃描@Controller,MVC子容器不掃描@Service.
事務配置如下:
spring-context.xml
<property name="dataSource" ref="dataSource" />
因爲spring容器和spring-mvc是父子容器,spring容器會先加載,如果此時掃描了Controller,但未掃描到Service。
spring事務配置文件還有上下文都是通過org.springframework.web.context.ContextLoaderListener加載的,而spring MVC的action是通過org.springframework.web.servlet.DispatcherServlet加載的 。
web是先啓動ContextLoaderListener後啓動DispatcherServlet 在ContextLoaderListener加載的時候action並沒在容器中,所以現在使用AOP添加事務或者掃描註解都是無用的。
結論:讓spring掃描註冊Service實現類,讓MVC掃描註冊Controller,此時spring父容器已經註冊Service爲Bean,此時事務可以得到正常配置。