Servlet监听器Listener

0、Listener是Servlet的一个功能组件:

(1)用于application,session,request三个对象创建、销毁

(2)向其内部添加、修改、删除属性时自动执行代码;

(3)可监听客户端的请求,服务端的操作等;

1、分为一下三类:》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

(1)ServletContext监听;

(2)Session监听

session超时配置(web.xml):

<session-config>
    <session-timeout>120</session-timeout><!--session120分钟后超时销毁-->
</session-config>

session无效:

public void invalidate();//使session失效方法。session.invalidate();

(3)Request监听;

2、在web.xml中配置:》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

扫描二维码关注公众号,回复: 433268 查看本文章

(1)Listener配置信息必须在Filter和Servlet配置之前;

(2)ServletContentListener比Servlet和Filter都优先,而销毁比Servlet和Filter都慢;

3、spring使用ContextLoaderListener加载ApplicationContext配置信息;》》》》》》》》》》》》

(1)启动Web容器时,自动装配ApplicationContext配置信息;

(2)实现了ServletContextListener这个接口;

(3)web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml";

(4)或自己配置:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:xml/spring-context.xml</param-value>
    </context-param>

4、Spring使用IntrospectorCleanupListener清理缓存:》》》》》》》》》》》》》》》》》》》》

(1)作用:在web应用关闭时刷新JDK的JavaBeans的Introspector缓存;

(2)刷新Introspector缓存可以确保Web应用程序的类加载器以及其加载的类正确的释放资源;

(3)Introspector被用来分析应用程序类,系统级的Introspector缓存将持有这些类的硬引用;

(4)硬引用使得这些类和Web应用程序的类加载器,在Web应用程序关闭时不会被垃圾收集器回收;

(5)IntrospectorCleanupListener清理硬引用,保证其可以被回收;

(6)刷新整个Introspector缓存是唯一清理方法;

(7)使用Spring内部的bean机制时,不需要使用此监听器,Spring本身不产生泄漏;

(8)简单Introspector泄漏将会导致整个Web应用程序的类加载器不会被回收;

(9)类加载器不会被回收导致:该应用程序所有的静态类资源(比如:单实例对象)都没有得到释放;

(10)IntrospectorCleanupListener应该注册为web.xml的第一个Listener,保证整个web应用生命周期;

猜你喜欢

转载自my.oschina.net/u/3847203/blog/1808786
今日推荐