web.xml 中 ServletListener 监听器作用

你的需求是当服务器启动后加载一些数据,我们就可以使用ServletContextListener来满足需求
这样获取是不可以的,当j2ee容器启动后会或获取一次spring上下文,如果使用该方式会在一次获取上下文。自己想想就知道.

ServletContextListener 不受spring管理我们应该如何获取呢?

实际上spring同样使用了ServletContextListener接口,我们可以通过实现一个自己的ServletContextListener
来得到spring上下文

作用:在启动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 类里 ,
如果你不知道为什么这里是程序的入口 , 那么复习一下 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() 方法被调用,所以在里面保存缓存的更改。将更改后的缓存保存回文件或者数据库,更新原来的内容。


你实现(implements) 了 ServletContextListener 编译后,把它放在正确的WEB-INF/classes 目录下,更改WEB-INF 目录下的 web.xml 文件,在web-app 节点里添加
<listener>
<listener-class>MyServletContextListener</listener-class>
</listener>
包+类名
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值