spring與springmvc父子容器


轉載地址:http://www.tianshouzhi.com/api/tutorials/spring

1、spring和springmvc父子容器概念介紹

在spring和springmvc進行整合的時候,一般情況下我們會使用不同的配置文件來配置spring和springmvc,因此我們的應用中會存在至少2個ApplicationContext實例,由於是在web應用中,因此最終實例化的是ApplicationContext的子接口WebApplicationContext。如下圖所示:

上圖中顯示了2個WebApplicationContext實例,為了進行區分,分別稱之為:Servlet WebApplicationContext、Root WebApplicationContext。 其中:

  • Servlet WebApplicationContext:這是對J2EE三層架構中的web層進行配置,如控制器(controller)、視圖解析器(view resolvers)等相關的bean。通過spring mvc中提供的DispatchServlet來加載配置,通常情況下,配置文件的名稱為spring-servlet.xml。
  • Root WebApplicationContext:這是對J2EE三層架構中的service層、dao層進行配置,如業務bean,數據源(DataSource)等。通常情況下,配置文件的名稱為applicationContext.xml。在web應用中,其一般通過ContextLoaderListener來加載。

 以下是一個web.xml配置案例:

<?xml version="1.0" encoding="UTF-8"?>  
  
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    
    <!—創建Root WebApplicationContext-->
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/spring/applicationContext.xml</param-value>  
    </context-param>  
  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    
    <!—創建Servlet WebApplicationContext-->
    <servlet>  
        <servlet-name>dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>dispatcher</servlet-name>  
        <url-pattern>/*</url-pattern>  
    </servlet-mapping>  
  
</web-app>

在上面的配置中:

  • 1、ContextLoaderListener會被優先初始化時,其會根據<context-param>元素中contextConfigLocation參數指定的配置文件路徑,在這里就是"/WEB-INF/spring/applicationContext.xml”,來創建WebApplicationContext實例。 並調用ServletContext的setAttribute方法,將其設置到ServletContext中,屬性的key為”org.springframework.web.context.WebApplicationContext.ROOT”,最后的”ROOT"字樣表明這是一個 Root WebApplicationContext。
  • 2、DispatcherServlet在初始化時,會根據<init-param>元素中contextConfigLocation參數指定的配置文件路徑,即"/WEB-INF/spring/spring-servlet.xml”,來創建Servlet WebApplicationContext。同時,其會調用ServletContext的getAttribute方法來判斷是否存在Root WebApplicationContext。如果存在,則將其設置為自己的parent。這就是父子上下文(父子容器)的概念。

        父子容器的作用在於,當我們嘗試從child context(即:Servlet WebApplicationContext)中獲取一個bean時,如果找不到,則會委派給parent context (即Root WebApplicationContext)來查找。
如果我們沒有通過ContextLoaderListener來創建Root WebApplicationContext,那么Servlet WebApplicationContext的parent就是null,也就是沒有parent context。

2、為什么要有父子容器

筆者理解,父子容器的作用主要是划分框架邊界。
        在J2EE三層架構中,在service層我們一般使用spring框架, 而在web層則有多種選擇,如spring mvc、struts等。因此,通常對於web層我們會使用單獨的配置文件。例如在上面的案例中,一開始我們使用spring-servlet.xml來配置web層,使用applicationContext.xml來配置service、dao層。如果現在我們想把web層從spring mvc替換成struts,那么只需要將spring-servlet.xml替換成Struts的配置文件struts.xml即可,而applicationContext.xml不需要改變。
        事實上,如果你的項目確定了只使用spring和spring mvc的話,你甚至可以將service 、dao、web層的bean都放到spring-servlet.xml中進行配置,並不是一定要將service、dao層的配置單獨放到applicationContext.xml中,然后使用ContextLoaderListener來加載。在這種情況下,就沒有了Root WebApplicationContext,只有Servlet WebApplicationContext。

3、相關問題

1.為什么不能在Spring的applicationContext.xml中配置全局掃描

如果都在spring容器中,這時的SpringMVC容器中沒有對象,所以加載處理器,適配器的時候就會找不到映射對象,映射關系,因此在頁面上就會出現404的錯誤。
因為在解析@ReqestMapping解析過程中,initHandlerMethods()函數只是對Spring MVC 容器中的bean進行處理的,並沒有去查找父容器的bean。因此不會對父容器中含有@RequestMapping注解的函數進行處理,更不會生成相應的handler。所以當請求過來時找不到處理的handler,導致404。

2.如果不用Spring容器,直接把所有層放入SpringMVC容器的配置spring-servlet.xml中可不可以

如果把所有層放入SpringMVC的。但是事務和AOP的相關功能不能使用(事務的時候,是在同一個容器才能達到事務作用,可能是因為sprigmvc容器事務相關沒有配置,這個還沒有驗證)。

3.同時掃描

會在兩個父子IOC容器中生成大量的相同bean,這就會造成內存資源的浪費。

一般正常的操作

因為@RequestMapping一般會和@Controller搭配使。為了防止重復注冊bean,建議在spring-servlet.xml配置文件中只掃描含有Controller bean的包,其它的共用bean的注冊定義到applicationContext.xml文件中。

applicationContext.xml

    <context:component-scan base-package="org.xuan.springmvc">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

spring-servlet.xml

    <context:component-scan base-package="org.xuan.springmvc.controller"
                            use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

注意

1.Spring容器導入的properties配置文件,只能在Spring容器中用而在SpringMVC容器中不能讀取到。 需要在SpringMVC 的配置文件中重新進行導入properties文件,並且同樣在父容器Spring中不能被使用,導入后使用@Value("${key}")在java類中進行讀取。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM