ApplicationContext和ServletContext

问题描述 投票:16回答:5

当谈到Spring MVC Application时,我对两个ApplicationContext和ServletContext感到困惑。我知道每个Spring Web应用程序只有一个ApplicationContext,每个Web应用程序也只有一个ServletContext。要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容。

这让我感到困惑。这两者之间有什么区别(我知道ApplicationContext有一些方法可以使用bean)?当我们使用ApplicationContext时何时使用ServletContext?

java spring spring-mvc servlets applicationcontext
5个回答
21
投票

Servlet Context:

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

Application Context:

这是一个特定于Spring的东西。它由Spring初始化。它包含spring配置文件中定义的bean的所有bean定义和生命周期。 Servlet-Context对此事情一无所知。

Spring父和子中有两种类型的上下文。

Spring Parent Context (Application Context / Root 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 当spring容器启动时,它会从配置文件中读取所有bean定义并创建bean对象并管理bean对象的生命周期。此配置完全是可选的。

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

Spring Child Context ( WebApplicationContext / Child Context )

<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>

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


11
投票

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

要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容。

如果你为此引用一个例子会有所帮助,因为据我所知,context-param用于ServletContext,而不是ApplicationContext。

更新:

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

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

0
投票

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

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

但是在没有包含任何框架的Plain J2EE Web应用程序的情况下,context-param可以从应用程序中的任何位置读取,即任何servlet,过滤器。

Sanjay解释说,ApplicationContext和ServletContext之间存在差异


0
投票

ApplicationContext是Spring的容器。

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

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

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


-1
投票

ServletContext与它的'封闭'ApplicationContext不同。 Java doc说下面的ServletContext

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

由于在同一个AppBase下可以有多个“Web应用程序”,每个都有自己的DocBaseWEB-INF/web.xml等,所以“Web应用程序”肯定有一个共同的环境/上下文,这被称为ApplicationContext。在JSF的情况下,PortletContextServletContext的反对部分,而ApplicationContext被称为ExternalContext

© www.soinside.com 2019 - 2024. All rights reserved.