2014-04-10 92 views
0

我跟着一些Spring MVC的教程和我的web.xml看起來是這樣的:在根上下文和servlet上下文中加載應用程序上下文有什麼好處嗎?

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 


     <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/servlet-context.xml</param-value> 


    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 


      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 

     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<session-config> 
    <session-timeout>1</session-timeout> 
</session-config> 

</web-app> 

我的問題是,什麼是裝載的servlet-context.xml中的根上下文和servlet上下文兩個好處?我是一個春季框架新手,我不太瞭解春季框架。

+0

爲了更好地理解在根和應用方面的區別就在這裏看我的答覆在http://stackoverflow.com/questions/19619539/understanding-contexts-in-spring-mvc/19621534# 19621534 – Shailendra

+0

另外,[this](http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s)。除了模塊分離以外,沒有任何實際的好處。 –

+0

@SotiriosDelimanolis:如果我從根上下文中刪除它會發生什麼?我的應用程序的工作方式會有什麼變化嗎? – riship89

回答

0

在Spring中,ApplicationContext可以是分層的。如果您在單個EAR中有多個Web應用程序,那麼EAR可以擁有自己的上下文,這是各個Web應用程序上下文的父項。同樣在每個Web應用程序中,您也可以擁有一個根上下文和各個子上下文。你可以在web.xml中定義這個層次結構。父上下文可以通過上下文參數來指定:locatorFactorySelector和parentContextKey。通過上下文參數contextConfigLocation的根上下文(context-param中的一個)。子上下文可以在每個servlet定義的init param - param-name屬性中指定。

在EAR中有一個jar包含所有常用服務和DAO層代碼,並在beanRefContext.xml(基本上是另一個應用程序上下文xml)中定義它們。使這個jar在類路徑中可用。

在要引用父上下文代碼中的每個應用程序的web.xml文件:

<!-- root application context --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:rootContextBeans.xml</param-value> 
</context-param> 
<!-- start shared service layer - parent application context --> 
<context-param> 
    <param-name>locatorFactorySelector</param-name> 
    <param-value>classpath:beanRefContext.xml</param-value> 
</context-param> 
<context-param> 
    <param-name>parentContextKey</param-name> 
    <param-value>servicelayer-context</param-value> 
</context-param> 
<!-- end shared service layer - parent application context --> 
<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener>  
<servlet> 
    <servlet-name>dispatcherServletApp1</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath*:webApp1.xml</param-value> 
    </init-param> 
</servlet> where beanRefContext.xml will be like: 

<beans> 
    <bean id="servicelayer-context" class="org.springframework.context.support.ClassPathXmlApplicationContext"> 
    <constructor-arg> 
     <list> 
     <value>data-layer-context.xml</value> 
     </list> 
    </constructor-arg> 
    </bean> 
</beans> 

如果你的項目沒有多的Web應用程序類型,那麼您不需要指定父應用程序上下文。您可以將通用服務和DAO層代碼,安全性移至可由dispatcherServlet bean訪問(可見)的根應用程序上下文(在上述web.xml中爲rootContextBeans.xml)(請記住反向可見性是不可能的)。
在您指定的web.xml中,根上下文contextConfigLocation和servlet contextConfigLocation中的servlet-context.xml。因此,您需要檢查在其中定義了哪些bean以及它的準確位置,並在其他位置刪除引用。

4

沒有很好的理由在servlet和根上下文中注入兩次完全相同的配置。大多數Spring MVC應用程序都有一個包含所有服務層/ DAO層bean的根上下文,以及應用程序的每個Spring調度程序servlet的一個servlet上下文,它包含(至少)每個servlet的控制器。

這個想法是,一個應用程序可能有幾個servlet調度程序,例如一個用於URL/desktop/*,另一個用於URL/mobile/*,每個都有自己的一組不同的控制器。

一個servlet調度器的控制器是相互隔離的,這意味着儘管它們也是Spring bean,但它們不能相互注入。

根上下文中的服務層和DAO bean在所有的servlet上下文中都是可見的,所以服務層bean可以注入到任何控制器中,而不是相反。

根上下文被認爲是控制器servlet上下文/上下文的父項。

這一切都意味着將一組bean彼此隔離,以確保不存在無限的依賴關係。

另外還有一些需要根上下文的Spring框架組件,例如OpenSessionInViewFilter

TLDR:不是,這是不可能在兩種情況下注入的servlet-context.xml中,但這不是它怎樣來使用:將有兩個獨立的環境中各類型的兩個豆,你也可以擁有交易應用,而另一個沒有應用等等,它可以很快產生難以排除故障的錯誤

相關問題