在根上下文和 servlet 上下文中加载应用程序上下文有什么好处吗? [英] is there any benefit of loading application context in root context and in servlet context?

查看:44
本文介绍了在根上下文和 servlet 上下文中加载应用程序上下文有什么好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I followed some spring mvc tutorial and my web.xml looks like this:

<?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框架不是很了解.

My question is, what is the benefit of loading servlet-context.xml in root context and in servlet context both? I am a spring framework newbie and I do not understand spring framework very well.

推荐答案

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

There is no good reason to inject the exact same configuration twice in both the servlet and the root context.

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

The idea for having multiple contexts is the following: Most Spring MVC applications have one root context containing all service layer / DAO layer beans, and one servlet context per spring dispatcher servlet of the application, which contains (at least) the controllers of each servlet.

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

The idea being that is that one application might have several servlet dispatchers, for example one for URL /desktop/* and the other for URL /mobile/*, each with it's own set of different controllers.

一个servlet dispatcher的controllers是相互隔离的,也就是说虽然也是Spring bean,但是不能相互注入.

The controllers of one servlet dispatcher are isolated from each other, meaning although they are also Spring beans, they cannot be injected in each other.

根上下文中的服务层和 DAO bean 在所有 servlet 上下文中都是可见的,因此服务层 bean 可以注入任何控制器,但不能反过来.

Service layer and DAO beans in the root context are visible in all servlet contexts, so Service layer beans can be injected in any controller, but not the other way around.

根上下文被认为是控制器 servlet 上下文/上下文的父级.

The root context is said to be the parent of the controller servlet context/contexts.

这一切都是为了将​​ bean 组彼此隔离,以确保不会产生无意义的依赖关系.

It's all meant to be a mechanism of isolating groups of beans from each other to ensure no unmeant dependencies are possible.

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

Also there are components of the Spring framework that need a root context, for example OpenSessionInViewFilter.

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

TLDR: it's not that it's impossible to inject servlet-context.xml in both contexts, but that is not how it's meant to be used: there will two beans of each type in two separate contexts, one can have transactions applied while the other doesn't, etc., it can quickly originate errors that are hard to troubleshoot

这篇关于在根上下文和 servlet 上下文中加载应用程序上下文有什么好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆