1. 程式人生 > >理解spring父子容器

理解spring父子容器

一、Spring和SpringMVC的父子容器關係

一般來說,我們在整合Spring和SpringMVC這兩個框架中,web.xml會這樣寫到:

<!-- 載入spring容器 --> <!-- 初始化載入application.xml的各種配置檔案 --> <context-param>    	<param-name>contextConfigLocation</param-name> 	<param-value>classpath:spring/application-*.xml</param-value>  
</context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置springmvc前端控制器 --> <servlet> <servlet-name>taotao-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置檔案預設在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param>
<load-on-startup>1</load-on-startup> </servlet>

首先配置的是Spring容器的初始化載入的application檔案,然後是SpringMVC的前端控制器(DispatchServlet),當配置完DispatchServlet後會在Spring容器中建立一個新的容器。其實這是兩個容器,Spring作為父容器,SpringMVC作為子容器。對於傳統的spring mvc來說,ServletDispatcher對應的容器為子容器,而web.xml中通過ContextLoaderListner的contextConfigLocation屬性配置的為父容器。 

這裡寫圖片描述

平時我們在專案中注入關係是這樣的順序(結合圖來說):在Service中注入Dao(初始化自動注入,利用@Autowired),接著在Controller裡注入Service(初始化自動注入,利用@Autowired),看圖,這就意味這作為SpringMVC的子容器是可以訪問父容器Spring物件的。

那麼問大家一個問題。要是反過來呢,你把Controller注入到Service中能行麼? 
肯定是不行的啊!(如圖,這也說明了父容器是不能呼叫子容器物件的)

如果Dao,Serive,Controller要是都在Spring容器中,無疑上邊的問題是肯定的,因為都是在一個bean裡,一個容器中。

1 .問題:為什麼不能在Spring中的Service層配置全域性掃描? 

例如:一個專案中我總專案的名字叫com.shop,我們在配置applicationContext-service.xml中,包掃描程式碼如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"     ...../ 此處省略>    <!-- 掃描包Service實現類 -->    <context:component-scan base-package="com.shop.service"></context:component-scan></beans>

上面所配置的是一個區域性掃描,而不是全域性掃描。接下來說原因: 
這裡就和上面講到的父子容器有關係,假設我們做了全域性掃描那麼程式碼如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"     ...../ 此處省略>    <!-- 掃描包Service實現類 -->    <context:component-scan base-package="com.shop"></context:component-scan></beans>

此時的Spring容器中就會掃描到@Controller,@Service,@Reposity,@Component,此時的圖如下這裡寫圖片描述 

結合圖去看,相當於他們都會放到大的容器中,而這時的SpringMVC容器中沒有物件,沒有物件就沒有Controller,所以載入處理器,介面卡的時候就會找不到對映物件,對映關係,因此在頁面上就會出現404的錯誤。

2.如果不用Spring容器,直接把所有層放入SpringMVC容器中可不可以?

當然可以,如果沒有Spring容器,我們是可以把所有層放入SpringMVC的。單獨使用這個容器是完全可以的,而且是輕量級的。

3.那麼為什麼我們在專案中還要聯合用到Spring容器和SpringMVC容器? 

答案是: Spring的擴充套件性,如果要是專案需要加入Struts等可以整合進來,便於擴充套件框架。如果要是為了快,為了方便開發,完全可以用SpringMVC框架。

4.元件分開掃描和直接全掃描的區別

分開掃描:

在主容器中(applicationContext.xml),將Controller的註解排除掉 
<context:component-scan base-package="com"> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
</context:component-scan> 

而在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>

因為spring的context是父子容器,所以會產生衝突,由ServletContextListener產生的是父容器,springMVC產生的是子容器,子容器Controller進行掃描裝配時裝配了@Service註解的例項,而該例項理應由父容器進行初始化以保證事務的增強處理,所以此時得到的將是原樣的Service(沒有經過事務加強處理,故而沒有事務處理能力。
 

還有一種方式是將service層改用xml配置,其實這樣做也是變相的讓springmvc無法掃描service,而只能依賴父視窗也就是ServletContextListener來進行初始化,這樣同樣被賦予了事務性。

 

直接掃描:

   直接掃描比較省事,但是事務回得不到處理,所以在具體的層面上還需要加入註解去宣告事務,比如在dao層和service層加入@Transactional