【问题标题】:ApplicationContext and ServletContextApplicationContext 和 ServletContext
【发布时间】:2015-11-03 02:13:53
【问题描述】:

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

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

【问题讨论】:

  • 你查过两者的 javadoc 了吗?
  • 我做了,但我无法通过,所以我问了这个问题。

标签: java spring spring-mvc servlets applicationcontext


【解决方案1】:

Servlet 上下文:

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

应用上下文:

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

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

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 子上下文(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>

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

【讨论】:

    【解决方案2】:

    它们是不同的东西。每个基于 Servlet 技术的 Java Web 应用程序都会有一个servlet context,无论它是不是 Spring 应用程序。相比之下,ApplicationContext 是 Spring 的东西;简单来说,就是一个装 Spring bean 的容器。

    为了同时初始化 ApplicationContext 和 ServletContext 的值,在 web.xml 中,我们将在 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>
    

    【讨论】:

    • 谢谢你的回答,桑杰。我读了一些文档说要在spring中为根应用程序上下文配置值,我们需要在web.xml的context-param中添加一些值。
    • 也许他们说您可以通过使用上下文参数来提供根应用程序上下文配置文件的位置。请参阅我的更新答案。
    【解决方案3】:

    ApplicationContext 是 Spring 的容器。

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

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

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

    【讨论】:

      【解决方案4】:

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

      但是对于不包含任何框架的普通 J2EE Web 应用程序,context-param 可以从应用程序中的任何位置读取,即任何 servlet、过滤器。

      ApplicationContextServletContext 的区别,sanjay 解释

      【讨论】:

        【解决方案5】:

        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

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        • 2015-04-26
        • 1970-01-01
        相关资源
        最近更新 更多