Ral*_*igh 23 java spring servlets spring-mvc applicationcontext
当谈到Spring MVC Application时,我对两个ApplicationContext和ServletContext感到困惑.我知道每个Spring Web应用程序只有一个ApplicationContext,每个Web应用程序也只有一个ServletContext.要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容.
这让我感到困惑.这两者之间有什么区别(我知道ApplicationContext有一些方法可以使用bean)?而当我们将使用的ApplicationContext和当我们将使用ServletContext的?
Par*_*ume 24
它在部署Servlet应用程序时初始化.Servlet Context包含整个servlet应用程序的所有配置(init-param,context-params等).
这是一个特定于Spring的东西.它由Spring初始化.它包含spring配置文件中定义的bean的所有bean定义和生命周期.Servlet-Context对此事情一无所知.
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>
Run Code Online (Sandbox Code Playgroud)
spring
-ContextLoaderListener-And-DispatcherServlet-Concepts
弹簧容器启动时,它从配置文件读取所有bean定义并创建bean对象并管理bean对象的生命周期.此配置完全是可选的.
DispatcherServlet vs 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>
Run Code Online (Sandbox Code Playgroud)
当spring web应用程序启动时,它将查找spring bean配置文件myWebApplication-servlet.xml.它将读取所有bean定义并创建和管理bean对象的生命周期.如果父弹簧上下文可用,它将子弹簧上下文与父弹簧上下文合并.如果没有可用的Spring父上下文,则应用程序将只具有子spring上下文.
San*_*jay 11
它们是分开的东西.每个基于Servlet技术的Java Web应用程序都将具有servlet上下文,无论它是否是spring应用程序.相比之下,ApplicationContext是一个Spring的东西; 简单来说,它是一个容纳Spring bean的容器.
要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容.
如果你为此引用一个例子会有所帮助,因为据我所知,context-param用于ServletContext,而不是ApplicationContext.
更新:
您可以使用a 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28563 次 |
| 最近记录: |