ApplicationContext和ServletContext [英] ApplicationContext and ServletContext

查看:108
本文介绍了ApplicationContext和ServletContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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-param tag.

这一点使我感到困惑.两者之间的有什么区别(我知道ApplicationContext有一些使用bean的方法)?和何时我们将使用 ApplicationContext 何时我们将使用 ServletContext ?

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

推荐答案

Servlet上下文:

在部署Servlet应用程序时初始化. Servlet上下文包含整个Servlet应用程序的所有配置(init-param,context-params等).

Servlet Context:

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.

这是Spring特有的东西.它由Spring初始化.它包含在Spring配置文件中定义的所有Bean定义和Bean的生命周期. Servlet-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父级和子级上下文有两种类型.

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

  <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
春季容器启动时,它将从配置文件中读取所有Bean定义,并创建Bean对象并管理Bean对象的生命周期. 此配置完全是可选的.

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.

DispatcherServlet与ContextLoaderListener
/declaring-spring-bean-in-parent -context-vs-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上下文和父级Spring上下文.如果没有可用的Spring父上下文,则应用程序将仅具有子Spring上下文.

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.

这篇关于ApplicationContext和ServletContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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