簡體   English   中英

在根上下文和servlet上下文中加載應用程序上下文有什么好處?

[英]is there any benefit of loading application context in root context and in servlet context?

我遵循了一些春季mvc教程,而我的web.xml如下所示:

<?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上下文中加載servlet-context.xml有什么好處? 我是Spring框架的新手,我對Spring框架不太了解。

沒有充分的理由在Servlet和根上下文中兩次注入完全相同的配置。

具有多個上下文的想法如下:大多數Spring MVC應用程序具有一個包含所有服務層/ DAO層bean的根上下文,以及該應用程序的每個Spring Dispatcher servlet一個Servlet上下文,該Servlet上下文包含(至少)每個Servlet的控制器。 。

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

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

根上下文中的服務層和DAO Bean在所有Servlet上下文中都是可見的,因此可以在任何控制器中注入服務層Bean,但不能以其他方式注入。

根上下文被稱為控制器Servlet上下文的父級。

所有這一切都是要使一組豆相互隔離的機制,以確保不會產生不協調的依賴關系。

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

TLDR :不是不可能在兩個上下文中都注入servlet-context.xml,而是這不是它的使用方式:在兩個單獨的上下文中將有兩種類型的bean,一種可以應用事務,而另一種則不需要等),它可以快速引發難以解決的錯誤

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

在EAR中有一個jar保存所有公共服務和DAO層代碼,並在beanRefContext.xml(基本上是另一個應用程序上下文xml)中定義它們。 使此jar在classpath中可用。

在每個要引用父上下文代碼的應用程序的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層代碼,安全性移至根應用程序上下文(在web.xml上方移至rootContextBeans.xml),可以由dispatcherServlet Bean訪問(可見)(記住,反向可見性是不可能的)。
在您的web.xml中,在根上下文contextConfigLocation和servlet contextConfigLocation中指定servlet-context.xml。 因此,您需要檢查其中定義了哪些bean,以及它完全適合的位置,並在其他位置刪除引用。

“ WEB-INF / spring”的根目錄中的所有Spring配置文件都被加載到Spring根目錄中。

此配置用於將與Web相關的任何Web加載到根上下文中,在這種情況下,這僅僅是Web安全性。

供參考: http : //www.springbyexample.org/examples/contact-webapp-spring-config.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM