Web应用中自动加载 ApplicationContext
对于 Web 应用,不必在代码中手动实例化 ApplicationContext。可通过 ContextLoader声明式地创建 ApplicationContext0ContextLoader有以下两个实现类:
ContextLoaderListener。
ContextLoaderServlet。
这两类功能相同,只是 listener不能在 Servlet2.2 兼容的容器中使用。根据 Servlet2.4规范, listener 会随 Web 应用启动时自动初始化,很多 Serlet 2.3 兼容的容器也提供该功能。
使用 ContextLoader Listener
使用 ContextLoaderListener 注册 ApplicationContext 的配置文件如下,注意:下面的配置文件不是在 Spring 的配置文件中增加,而是在 web.xm1文件中增加。
<!确定配置文件的位置--〉
<context-param><param-name>contextConfigLocation</param-name>
<!--此处可以列出多个Spring 的 XML 配置文件→
<param-value>/WEB-INF/daoContext.xml/WEB-iNF/applicationContext.xml</
param-value></context-param>
<!-- 应用启动时,自动加载listener,该 listener会读取上面确定的XML配置文件。
然后创建ApplicationContext实例--〉
<listener>
<listener-class>org.springframework.web.context.ContextLoader
Listener</listener-class>
</listener>
|
使用 ContextLoaderServlet
使用 ContextLoaderServlet注册 ApplicationContext的配置文件如下。同样,下面的配置文件也不是在Spring的配置文件中增加,而是在web.xm1文件中增加。
<servlet>
<!--确定Servlet 的名-->
<servlet-name>context</servlet-name><!--确定 Servlet对应的类--〉
<servlet-class>org.springframework.web.context.ContextLoaderServlet</
servlet-class>
<!--确定Servlet的启动级别--〉
<load-on-startup>l</load-on-startup></servlet>
|
采用这种方式时,应将context 的启动级别设成最小,即最优先启动。因为ApplicationContext是整个应用的核心。
注意:在两种启动方式中,推荐采用第一种。因为根据Servlet2.4规范, listener比Servlet优先启动;关键问题是有些容器并不支持Serlet2.4规范,即不支持listener。支持 listener的容器有:
ApacheTomcat4.x 及更高版本。
Jetty4.x及更高版本。
Resin 2.1.8 及更高版本。
Orion2.0.2及更高版本。
BEAWebLogic8.1 SP3
不支持 listener 的容器有:
BEAWebLogicupto 8.1 SP2 及更低版本。
IBMWebSphere 5.x 及更低版本。
OracleOC4J9.0.3 及更低版本。
分享到:
相关推荐
通过以上配置,Web 容器会自动加载 /WEB-INF/applicationContext.xml 初始化 ApplicationContext 实例,如果需要指定配置文件位置,可通过 context-param 加以指定: ```xml <param-name>contextConfigLocation ...
在实际应用中,为了更好地管理项目的生命周期和资源,Spring提供了多种自动加载机制来帮助开发者更加便捷地初始化和配置Spring环境。本文将详细介绍Spring自动加载的相关知识点,并通过具体的示例来加深理解。 ####...
本文主要围绕"Spring源码学习七:web应用自动装配Spring配置文件1"这一主题,详细解析Web环境中Spring的初始化过程。 首先,我们注意到在传统的Java应用程序中,通常使用`ClassPathXmlApplicationContext`手动创建...
在Web应用启动时,如果在web.xml文件中定义了Servlet,那么Web容器(如Tomcat)会加载并初始化这些Servlet。我们可以通过重写Servlet的`init()`方法来执行启动时的任务。 在描述中提到,我们需要在Servlet的`init()...
- 自动注册监听器,以便在Web应用启动时初始化`ApplicationContext`。 - 提供了`ServletContext`作为其父上下文,从而可以访问Web应用的资源和属性。 - **示例代码**: ```java ServletContext servletContext ...
`<listener>`标签中的`<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>`定义了一个监听器,它会在Web应用启动时自动加载默认的Spring配置文件,即`/WEB-INF/...
- **ContextLoaderListener**:这是一个Servlet监听器,它会在Web应用启动时自动加载`/WEB-INF/applicationContext.xml`(默认配置)。 - **ContextLoaderServlet**:这是一个Servlet,同样用于初始化`...
`ContextLoaderListener`会在Web应用启动时自动加载配置并创建ApplicationContext。 ```xml <web-app> ... <listener-class>org.springframework.web.context.ContextLoaderListener <param-name>...
这个监听器是基于Servlet容器(如Tomcat、Jetty等)的,当Web应用启动时,它会读取配置文件(通常是`web.xml`),创建并加载ApplicationContext。如果在这个过程中遇到问题,比如`ClassNotFoundException`,那通常...
`ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`常量用于在`ServletContext`中存储根`WebApplicationContext`的引用,这样Web应用的其他部分就可以找到并使用这个容器。 `XmlWebApplicationContext`是`...
10. **MVC配置**:对于Web应用,`applicationContext.xml`常与`servlet-context.xml`配合,定义DispatcherServlet的配置,处理HTTP请求。 以上只是`applicationContext.xml`在Spring 2.5中部分核心功能的概述。实际...
在Spring框架中,`ApplicationContext`是核心容器的重要接口,它负责管理Bean的生命周期,加载配置元数据,并提供Bean之间的依赖注入。本示例主要探讨`ApplicationContext`及其几个常见子类的使用,通过实际的代码...
Spring提供了一个名为`ContextLoaderListener`的监听器,它自动加载配置文件,创建ApplicationContext,并将其存储到Servlet上下文(ServletContext)中。这样,我们可以通过`WebApplicationContextUtils`工具类的...
SSM框架,全称Spring、SpringMVC和MyBatis,是Java开发中常用的一种轻量级Web应用程序开发框架。这个压缩包包含的基础配置文件是SSM整合的关键部分,可以帮助开发者快速搭建项目结构,减少重复劳动。以下是这些配置...
ContextLoaderListener 是Spring框架中一个重要的组件,它作为Servlet容器的监听器(ServletContextListener),在Web应用程序启动时自动加载ApplicationContext的配置信息。当web.xml中配置了`<listener-class>org...
当Web应用启动时,这些自定义类会被自动加载和执行特定的方法。 - **配置示例**: ```xml <web-app version="2.5" xmlns=...
`ContextLoaderListener`是Spring框架提供的一个监听器,其主要功能是在Web应用启动时自动加载`ApplicationContext`的配置信息。通过在`web.xml`中配置如下: ```xml <listener-class>org.springframework....
1. **设置正确的类路径**:确保包含Applet及其依赖的所有JAR文件都在Web应用的WEB-INF/lib目录下。这样,当Applet被加载时,Web服务器会自动将其包含在类路径中。 2. **使用标签**:在HTML的标签中,可以使用标签来...
- **配置Servlet容器**: 如果是使用Servlet容器(如Tomcat),需要配置一个名为`ContextLoaderListener`的监听器,它会在Web应用启动时加载Spring的ApplicationContext。 - **创建Spring配置文件**: 创建XML或Java...