Java ApplicationContext 和 ServletContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31931848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:47:49  来源:igfitidea点击:

ApplicationContext and ServletContext

javaspringspring-mvcservletsapplicationcontext

提问by Raleigh

I get confused between the two ApplicationContext and ServletContext when it comes to Spring MVC Application. I know that There is just only One ApplicationContext per Spring Web Application and there is also just only One ServletContext per web application. To initiate the value for both ApplicationContext and ServletContext, in web.xml, we will add something in context-paramtag.

当涉及到 Spring MVC 应用程序时,我在两个 ApplicationContext 和 ServletContext 之间感到困惑。我知道每个 Spring Web 应用程序只有一个 ApplicationContext,每个 Web 应用程序也只有一个 ServletContext。为了初始化 ApplicationContext 和 ServletContext 的值,在 web.xml 中,我们将在context-param标签中添加一些东西。

That is the point that makes me confused. What are the differencesbetween these two (i know ApplicationContext has some methods to work with beans)? and Whenwe would use ApplicationContextand Whenwe would use ServletContext?

这就是让我感到困惑的一点。这两者之间有什么区别(我知道 ApplicationContext 有一些处理 bean 的方法)?而我们将使用的ApplicationContext我们将使用ServletContext的

回答by Sanjay

They are separate things. Every Java web applications based on Servlet technology will have a servlet context, whether it's a spring application or not. In contrast, the ApplicationContextis a Spring thing; in very simple terms, it's a container to hold Spring beans.

它们是分开的东西。每个基于 Servlet 技术的 Java Web 应用程序都会有一个servlet 上下文,无论它是否是 Spring 应用程序。相比之下,ApplicationContext是 Spring 的东西;简单来说,它是一个容纳 Spring bean 的容器。

To initiate the value for both ApplicationContext and ServletContext, in web.xml, we will add something in context-param tag.

为了初始化 ApplicationContext 和 ServletContext 的值,在 web.xml 中,我们将在 context-param 标签中添加一些东西。

It would help if you quote an example for this, because, as far as I know, context-param is used for ServletContext, and not ApplicationContext.

如果您为此引用一个示例会有所帮助,因为据我所知,上下文参数用于 ServletContext,而不是 ApplicationContext。

Update:

更新

You can use a context-paramto provide the locations of the root application context configuration files, as below.

您可以使用 acontext-param来提供根应用程序上下文配置文件的位置,如下所示。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/root-context.xml
        /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>

回答by Ankireddy Polu

In Spring, to read a specific initialisation configuration file, we use the context-paramwith the predefined name called contextConfigLocation.

在 Spring 中,为了读取特定的初始化配置文件,我们使用具有名为contextConfigLocation的预定义名称的context-param

<context-param>
  <description>WebFlow context configuration</description>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/test-context.xml</param-value>
</context-param> 

But in case of Plain J2EE web application without including any frameworks, the context-paramcan read from any where in the application i.e. any servlet, filter.

但是在没有包含任何框架的普通 J2EE Web 应用程序的情况下,上下文参数可以从应用程序中的任何位置读取,即任何 servlet、过滤器。

The difference between ApplicationContextand ServletContext, sanjay explained

ApplicationContextServletContext的区别,sanjay 解释

回答by ParagFlume

Servlet Context:

Servlet 上下文:

It is initialized when an Servlet application is deployed. Servlet Context holds all the configurations (init-param, context-params etc) of the whole servlet application.

它在部署 Servlet 应用程序时初始化。Servlet Context 包含整个 servlet 应用程序的所有配置(init-param、context-params 等)。

Application Context:

应用上下文:

It is a Spring specific thing. It is initialized by Spring. It holds all the bean definitions and life-cycle of the of the beans that is defined inside the spring configuration files. Servlet-Context has no idea about this things.

这是 Spring 特有的东西。它由 Spring 初始化。它保存在 spring 配置文件中定义的 bean 的所有 bean 定义和生命周期。Servlet-Context 不知道这件事。

There are two types of contexts in Spring parent and child.

Spring parent 和 child 中有两种类型的上下文。

Spring Parent Context (Application Context / Root Context )

Spring Parent Context(应用程序上下文/根上下文)

  <listener>
        <listener-lass> 
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/service-context.xml,
            /WEB-INF/dao-context.xml,
            /WEB-INF/was-context.xml,
            /WEB-INF/jndi-context.xml,
            /WEB-INF/json-context.xml
        </param-value>
  </context-param>

role-purpose-of-contextloaderlistener-in-spring
Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
When spring container start ups, it reads all the bean definitions from the configuration files and creates beans objects and manages life cycle of the beans objects. This configuration is totally optional.

role-purpose-of-contextloaderlistener-in-spring
Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
当spring容器启动时,它从配置文件中读取所有bean定义并创建bean对象并管理bean对象的生命周期。此配置完全是可选的。

DispatcherServlet vs ContextLoaderListener
/declaring-spring-bean-in-parent-context-vs-child-context

DispatcherServlet vs ContextLoaderListener
/declaring-spring-bean-in-parent-context-vs-child-context

Spring Child Context ( WebApplicationContext / Child Context )

Spring 子上下文(WebApplicationContext/子上下文)

<servlet>
    <servlet-name>myWebApplication</servlet-name>
    <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myWebApplication</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

When spring web application starts it will look for spring bean configuration file myWebApplication-servlet.xml. It will read all the bean definitions and create and manages the bean objects life cycle. If parent spring context is available it will merge the child spring context with the parent spring context. If there is no Spring parent context available the application will only have the child spring context.

当 spring web 应用程序启动时,它会查找 spring bean 配置文件 myWebApplication-servlet.xml。它将读取所有 bean 定义并创建和管理 bean 对象生命周期。如果父 spring 上下文可用,它会将子 spring 上下文与父 spring 上下文合并。如果没有可用的 Spring 父上下文,则应用程序将只有子 Spring 上下文。

回答by Th?nh Nguyên

ApplicationContext is Spring's container.

ApplicationContext 是 Spring 的容器。

It's used to wire the configurations from Spring beans together and use them for the application.

它用于将来自 Spring bean 的配置连接在一起并将它们用于应用程序。

Use ApplicationContext if you want to retrieve the information of Spring beans.

如果要检索 Spring bean 的信息,请使用 ApplicationContext。

Use ServletContext if you want to get/set attribute those shared to all Servlet.

如果要获取/设置所有 Servlet 共享的属性,请使用 ServletContext。

回答by Madhu

ServletContextis distinguished from it's 'enclosing' ApplicationContext. The Java doc says the below for ServletContext

ServletContext区别于它的 'enclosure' ApplicationContext。Java 文档如下所述ServletContext

There is one [servlet] context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

每个 Java 虚拟机的每个“Web 应用程序”有一个 [servlet] 上下文。(“Web 应用程序”是安装在服务器 URL 命名空间的特定子集(例如 /catalog)下并可能通过 .war 文件安装的 servlet 和内容的集合。)

Since there can be more than one "web application" under the same AppBase, each with their own DocBase, WEB-INF/web.xmletc., there is definitely a common environment/context that is shared by all "web applications", which is being referred to as the ApplicationContext. In case of JSF, PortletContextis the counter part of ServletContextand the ApplicationContextis referred to as the ExternalContext.

由于可以有在相同的一个以上的“web应用程序” AppBase,每个都有自己的DocBaseWEB-INF/web.xml等等,肯定是受所有的“web应用程序”,这被称为共享的公共环境/上下文ApplicationContext。在JSF的情况下,PortletContext是的计数器部分ServletContextApplicationContext被作为称作ExternalContext