ApplicationContext和ServletContext
当谈到Spring MVC Application时,我对两个ApplicationContext和ServletContext感到困惑。 我知道每个Spring Web应用程序只有一个ApplicationContext,每个Web应用程序也只有一个ServletContext。 要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容。
这让我感到困惑。 这两者之间有什么区别 (我知道ApplicationContext有一些使用bean的方法)? 当我们使用ApplicationContext 时何时使用ServletContext ?
Servlet上下文:
它在部署servlet应用程序时启动。 Servlet Context包含整个servlet应用程序的所有配置(init-prarm,context-params等)。
应用背景:
是一个spring的事情。 它由Spring发起。 它包含在spring配置文件中定义的bean的所有bean定义和生命周期。 Servlet-Context对此事情一无所知。
spring父母和孩子有两种类型的背景。
Spring父上下文(应用程序上下文/根上下文)
org.springframework.web.context.ContextLoaderListener contextConfigLocation /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
角色专用的-的ContextLoaderListener式弹簧
弹簧的ContextLoaderListener和-的DispatcherServlet的概念
当spring容器启动时,它从配置文件中读取所有bean定义并创建bean对象并管理bean对象的生命周期。 这种配置完全是可选的。
DispatcherServlet vs ContextLoaderListener
/宣称-弹簧-豆在父上下文-VS-孩子上下文
Spring子上下文(WebApplicationContext / Child Context)
myWebApplication org.springframework.web.servlet.DispatcherServlet 1 jobbuzz-web /app/*
当spring web应用程序启动时,它将查找spring bean configration文件myWebApplication-servlet.xml。 它将读取所有bean定义并创建和管理bean对象的生命周期。 如果父弹簧上下文可用,它将子弹簧上下文与父弹簧上下文合并。 如果没有spring父上下文,则应用程序将只具有子spring上下文。
它们是分开的东西。 每个基于Servlet技术的Java Web应用程序都将具有servlet上下文 ,无论它是否为spring应用程序。 相比之下, ApplicationContext是一个Spring的东西; 简单来说,它是一个容纳Spring bean的容器。
要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容。
如果你为此引用一个例子会有所帮助,因为据我所知,context-param用于ServletContext,而不是ApplicationContext。
更新 :
您可以使用context-param
来提供根应用程序上下文配置文件的位置,如下所示。
contextConfigLocation /WEB-INF/root-context.xml /WEB-INF/applicationContext-security.xml
在Spring中,为了读取特定的初始化配置文件,我们使用context-param和名为contextConfigLocation的预定义名称。
WebFlow context configuration contextConfigLocation /WEB-INF/test-context.xml
但是在没有包含任何框架的Plain J2EE Web应用程序的情况下, context-param可以从应用程序中的任何位置读取,即任何servlet,filter。
Sanjay解释说, ApplicationContext和ServletContext之间存在差异
ApplicationContext是Spring的容器。
它用于将Spring bean中的配置连接在一起,并将它们用于应用程序。
如果要检索Spring bean的信息,请使用ApplicationContext。
如果要获取/设置共享给所有Servlet的属性,请使用ServletContext。