2
作用:在启动Web
容器时,自动装配Spring
applicationContext.xml
的配置信息。
因为它实现了ServletContextListener
这个接口,在web.xml
配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener
中关联了ContextLoader
这个类,所以整个加载配置过程由ContextLoader
来完成
pring
在
web
下的入口在配置文件
web.xml
的监听器中
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/applicationContext.xml</param-value>
</context-param>
上述是在web.xml
中的配置信息。
//
实现了接口
ServletContextListener,
也就是说他必须实现
contextDestroyed, contextInitialized
这两个方法
public
class ContextLoaderListener implements ServletContextListener
{
private
ContextLoader contextLoader;
/**
*
Initialize the root web application context.
*/
//Spring
框架由此启动
, contextInitialized
也就是监听器类的
main
入口函数
public
void contextInitialized
(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create
the ContextLoader to use. Can be overridden in subclasses.
* @return
the new ContextLoader
*/
protected
ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return
the ContextLoader used by this listener.
* @return
the current ContextLoader
*/
public
ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close
the root web application context.
*/
public
void contextDestroyed
(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
}
总的来说这个入口非常简单
,
所有实现都隐藏在
ContextLoader
类里
,
我们在下一篇的内容中讨论
ContextLoader,
如果你不知道为什么这里是程序的入口
,
那么复习一下
ServletContextListener
接口和监听器的相关知识吧
ServletContext
被
Servlet
程序用来与 Web
容器通信。例如写日志,转发请求。每一个 Web
应用程序含有一个Context
,被Web
应用内的各个程序共享。因为Context
可以用来保存资源并且共享,所以我所知道的 ServletContext
的最大应用是Web
缓存----
把不经常更改的内容读入内存,所以服务器响应请求的时候就不需要进行慢速的磁盘I/O
了。
ServletContextListener
是
ServletContext
的监听者,如果 ServletContext
发生变化,如服务器启动时 ServletContext
被创建,服务器关闭时 ServletContext
将要被销毁。
在JSP
文件中,application
是 ServletContext
的实例,由JSP
容器默认创建。Servlet
中调用 getServletContext()
方法得到 ServletContext
的实例。
我们使用缓存的思路大概是:
1.
服务器启动时,ServletContextListener
的
contextInitialized()
方法被调用,所以在里面创建好缓存。可以从文件中或者从数据库中读取取缓存内容生成类,用 ervletContext.setAttribute()
方法将缓存类保存在
ServletContext
的实例中。
2.
程序使用 ServletContext.getAttribute()
读取缓存。如果是 JSP
,使用a pplication.getAttribute()
。如果是 Servlet
,使用 getServletContext().getAttribute()
。如果缓存发生变化(
如访问计数)
,你可以同时更改缓存和文件/
数据库。或者你等 变化积累到一定程序再保存,也可以在下一步保存。
3.
服务器将要关闭时,ServletContextListener
的 contextDestroyed()
方法被调用,所以在里面保存缓存的更改。将更改后的缓存保存回文件或者数据库,更新原来的内容。
Java
代码
-
import User; //my own
-
classimport DatabaseManager; // my own class
-
import javax.servlet.ServletContext;
-
import javax.servlet.ServletContextListener;
-
public class MyContextListener implements ServletContextListener {
-
private ServletContext context = null;
-
public void contextInitialized(ServletContextEvent event) {
-
context = event.getServletContext();
-
User user = DatabaseManager.getUserById(1);
-
context.setAttribute("user1", user);
-
}
-
public void contextDestroyed(ServletContextEvent event) {
-
User user = (User)context.getAttribute("user1");
-
DatabaseManager.updateUserData(user);
-
this.context = null;
-
}
-
}
import User; //my own
classimport DatabaseManager; //
my own class
import
javax.servlet.ServletContext;
import
javax.servlet.ServletContextListener;
public class
MyContextListener
implements
ServletContextListener {
private ServletContext context = null;
public void
contextInitialized(ServletContextEvent event) {
context =
event.getServletContext();
User user =
DatabaseManager.getUserById(1);
context.setAttribute("user1", user);
}
public void
contextDestroyed(ServletContextEvent event) {
User user =
(User)context.getAttribute("user1");
DatabaseManager.updateUserData(user);
this.context = null;
}
}
布署 ServletContextListener
你实现(implements)
了 ServletContextListener
编译后,把它放在正确的WEB-INF/classes
目录下,更改WEB-INF
目录下的 web.xml
文件,在web-app
节点里添加
<listener>
<listener-class>MyServletContextListener</listener-class>
</listener>
分享到:
相关推荐
Spring的监听器ContextLoaderListener的作用
二、 使用 Spring 的器监听器 ContextLoaderListener o1. maven依赖pom.xml o2. 注册监听器 ContextLoaderListener o3. 指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先...
java解决org.springframework.web.context.ContextLoaderListener
struts2与spring的整合。导入struts2-spring-... spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,
-- Spring 容器启动监听器 --> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml...
3.配置spring的监听器: 在web.xml中添加 <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 4.配置spring的配置文件(可以查看WEB-INF里面的...
1,在web.xml配置监听器(Spring Reference 15.2 Common configuration) <!-- 集成Spring --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <context-...
下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...
文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...
1.项目管理和综合工具:maven(1)Maven安装和配置:参考(2)Eclipse和IDEA配置Maven2.Spring版本:5.0.73.MVC:Spring MVC配置(1)jar包:spring-webmvc.jar(2)web.xml配置SpringMVC监听类:org.springframework.web....
本文介绍了Listener以下几个方面的内容: ... Spring使用ContextLoaderListener加载ApplicationContext配置信息 Spring使用Log4jConfigListener配置Log4j日志 Spring使用IntrospectorCleanupListener清理