原因:
由ServletContextListener加載spring配置文件產生的是父容器,springMVC產生的是子容器,子容器對Controller進行掃描裝配時裝配了@Service注解的實例,而該實例理應由父容器進行初始化以保證事務的增強處理。所以此時得到的將是原樣的Service(沒有經過事務加強處理),故而沒有事務處理能力。
第一種解決辦法:
1.在主容器中(applicationContext.xml),將Controller的注解排除掉:
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
2.而在springMVC配置文件中將Service注解給去掉:
<context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
第二種解決辦法:
將service層改用xml配置,其實這樣做也是變相的讓springmvc無法掃描service,而只能依賴父窗口也就是ServletContextListener來進行初始化,這樣同樣被賦予了事務性。
Spring和SpringMVC的父子容器關系
1.講問題之前要先明白一個關系
一般來說,我們在整合spring和SpringMVC這兩個框架中,web.xml會這樣寫到:
1 <!-- 加載spring容器 --> 2 <!-- 初始化加載application.xml的各種配置文件 --> 3 <context-param> 4 <param-name>contextConfigLocation</param-name> 5 <param-value>classpath:spring/application-*.xml</param-value> 6 </context-param> 7 <listener> 8 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 9 </listener> 10 11 <!-- 配置springmvc前端控制器 --> 12 <servlet> 13 <servlet-name>taotao-manager</servlet-name> 14 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 15 <!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, 16 springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" --> 17 <init-param> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>classpath:spring/springmvc.xml</param-value> 20 </init-param> 21 <load-on-startup>1</load-on-startup> 22 </servlet>
首先配置的是Spring容器的初始化加載的application文件,然后是SpringMVC的前端控制器(DispatchServlet),當配置完DispatchServlet后會在Spring容器中創建一個新的容器。其實這是兩個容器,Spring作為父容器,SpringMVC作為子容器。
讓我們用圖來看一下這個父子關系的原理:
平時我們在項目中注入關系是這樣的順序(結合圖來說):在Service中注入Dao(初始化自動注入,利用@Autowired),接着在Controller里注入Service(初始化自動注入,利用@Autowired),看圖,這就意味這作為SpringMVC的子容器是可以訪問父容器Spring對象的。
那么問大家一個問題。要是反過來呢,你把Controller注入到Service中能行么?
肯定是不行的啊!(如圖,這也說明了父容器是不能調用子容器對象的)
如果Dao,Serive,Controller要是都在Spring容器中,無疑上邊的問題是肯定的,因為都是在一個bean里,一個容器中。
2.如果只用Spring容器,直接把所有層放入Spring容器中可不可以?
例如:一個項目中我總項目的名字叫com.shop,我們在配置applicationContext-service.xml中,包掃描代碼如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 ...../ 此處省略> 5 6 <!-- 掃描包Service實現類 --> 7 <context:component-scan base-package="com.shop.service"></context:component-scan> 8 </beans>
上面所配置的是一個局部掃描,而不是全局掃描。接下來說原因:
這里就和上面講到的父子容器有關系,假設我們做了全局掃描那么代碼如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 ...../ 此處省略> 5 6 <!-- 掃描包Service實現類 --> 7 <context:component-scan base-package="com.shop"></context:component-scan> 8 </beans>
此時的Spring容器中就會掃描到@Controller,@Service,@Reposity,@Component,此時的圖如下:
結合圖去看,相當於他們都會放到大的容器中,而這時的SpringMVC容器中沒有對象,沒有對象就沒有Controller,所以加載處理器,適配器的時候就會找不到映射對象,映射關系,因此在頁面上就會出現404的錯誤。
3.如果不用Spring容器,直接把所有層放入SpringMVC容器中可不可以?
當然可以,如果沒有Spring容器,我們是可以把所有層放入SpringMVC的。單獨使用這個容器是完全可以的,而且是輕量級的。就是直接把所有的層次關系都放到了SpringMVC中,並沒有用到Spring容器。
4.那么為什么我們在項目中還要聯合用到Spring容器和SpringMVC容器?
答案是:Spring的擴展性,如果要是項目需要加入Struts等可以整合進來,便於擴展框架。如果要是為了快,為了方便開發,完全可以用SpringMVC框架。
疑問一: 單例的bean在父子容器中存在一個實例還是兩個實例?
答:初始化兩次,Spring 容器先初始化bean,MVC容器再初始化bean,所以應該是兩個bean。
疑問二:為啥不把所有bean 都在子容器中掃描?
答: 網上很多文章說子容器不支持AOP,其實這是不對的。因為正常會有AOP的相關配置都在Spring容器中配置,如果都遷移到MVC配置文件,則所有bean都在子容器中,相當於只有一個容器了,所以也就實現了AOP。缺點是不利於擴展。
5.以上是傳統型SSM(spring+springmvc+mybatis)配置時,父類容器負責掃描裝配service層和dao層,子類容器負責掃描裝配controller層,但是在輕量化的javaEE開發中,我們可以不用spring框架,只是利用spinrgmvc+mybatis即可
具體操作:不使用listener監聽器來加載spring的配置文件,只使用DispatcherServlet來加載spring的配置,不要父子上下文,只使用一個DispatcherServlet。將原本應該放在父容器中的數據源、服務層、DAO層、事務的Bean、以及子容器controller的Bean都放在子上下文容器中,這樣也可以實現事務。