ApplicationContext和ServletContext

时间:2015-08-11 01:38:31

标签: java spring spring-mvc servlets applicationcontext

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

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

5 个答案:

答案 0 :(得分:19)

Servlet上下文:

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

应用程序上下文:

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

Spring父母和子女有两种类型的情境。

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

  <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>jobbuzz-web</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

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

答案 1 :(得分: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>

答案 2 :(得分: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,过滤器。

ApplicationContext ServletContext 之间的区别,sanjay解释

答案 3 :(得分:0)

ApplicationContext是Spring的容器。

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

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

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

答案 4 :(得分:-1)

ServletContext与它的“封闭” ApplicationContext有所区别。 Java文档对ServletContext

说了以下内容
  

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

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