【问题标题】:is there any benefit of loading application context in root context and in servlet context?在根上下文和 servlet 上下文中加载应用程序上下文有什么好处吗?
【发布时间】:2014-05-24 14:41:33
【问题描述】:

我遵循了一些 spring 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框架不是很了解。

【问题讨论】:

  • 为了更好地理解根和应用程序上下文的区别,请参阅我在stackoverflow.com/questions/19619539/…的回复
  • 另外,this。除了模块分离之外没有其他真正的好处。
  • @SotiriosDelimanolis:如果我从根上下文中删除它会发生什么?我的应用程序的工作方式会有任何变化吗?
  • @riship89 很少有东西可能会损坏。 BeanFactoryPostProcessors 和 BeanPostProcessors 仅适用于定义它们的上下文。

标签: xml spring spring-mvc web-applications


【解决方案1】:

没有充分的理由在 servlet 和根上下文中两次注入完全相同的配置。

拥有多个上下文的想法如下:大多数 Spring MVC 应用程序有一个包含所有服务层/DAO 层 bean 的根上下文,以及每个应用程序的 spring 调度程序 servlet 一个 servlet 上下文,其中包含(至少)控制器每个 servlet。

这个想法是一个应用程序可能有多个 servlet 调度程序,例如一个用于 URL /desktop/*,另一个用于 URL /mobile/*,每个都有自己的一组不同的控制器。

一个 servlet dispatcher 的控制器是相互隔离的,这意味着虽然它们也是 Spring bean,但它们不能相互注入。

根上下文中的服务层和 DAO bean 在所有 servlet 上下文中都是可见的,因此可以将服务层 bean 注入任何控制器,但反之则不行。

根上下文被称为控制器 servlet 上下文/上下文的父级。

这一切都是为了成为一种将 bean 组彼此隔离的机制,以确保不会出现无意义的依赖关系。

还有一些 Spring 框架的组件需要根上下文,例如 OpenSessionInViewFilter

TLDR:并不是说在两个上下文中都注入 servlet-context.xml 是不可能的,但这不是它的用途:在两个不同的上下文中,每种类型都有两个 bean ,一个可以应用事务而另一个不应用,等等,它可以快速产生难以排除的错误

【讨论】:

    【解决方案2】:

    在 Spring 中,ApplicationContext 可以是分层的。如果您在单个 EAR 中有多个 webapp,EAR 可以有自己的上下文,它是各个 webapp 上下文的父级。同样在每个 webapp 中,您也可以拥有一个根上下文和单独的子上下文。您可以在 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>
    

    如果您的项目没有多网络应用程序类型,那么您不需要指定父应用程序上下文。您可以将公共服务和 DAO 层代码、安全性移动到根应用程序上下文(在上面的 web.xml 到 rootContextBeans.xml),可以通过 dispatcherServlet bean 访问(可见)(记住反向可见性是不可能的)。
    在您指定的 web.xml 中,servlet-context.xml 在根上下文 contextConfigLocation 和 servlet contextConfigLocation 中。因此,您需要检查其中定义了哪些 bean 以及它完全适合的位置,并删除其他地方的引用。

    【讨论】:

      【解决方案3】:

      'WEB-INF/spring' 根目录下的所有 Spring 配置文件都加载到根 Spring 上下文中。

      此配置用于将任何与 Web 相关的内容加载到根上下文中,在这种情况下,它只是 Web 安全性。

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

      【讨论】:

        猜你喜欢
        • 2019-08-13
        • 2010-09-09
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 2010-12-21
        相关资源
        最近更新 更多