java 中个人监听器的总结以及监听器的配置
监听器的总结
SSM框架中 配置监听器ContextLoaderListener(如果框架正常运行,那么监听器不需要导包) 配置环境上下文载入监听器
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息 (自动装配应用上下文).
配置如下
在配置过程中监听器的位置应当在过滤器的后面servle的前面
在wen.xml文件中:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springMvc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
注:<param-value>路径问题:
首相有两种路径:
classpath 是相对于 classes 目录或者查找 jar 包里的文件
/WEB-INF 这个是相对于项目路径
(根据配置的框架不同,如果框架配置中有applicationContext.xml则<param-value>中写applicationContext.xml路径)没有的话可以是Spring。xml或者是SpringMVC.xml
则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。
但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。
1: <param-value>classpath:applicationContext.xml</param-value>
2:<param-value>/WEB-INF/applicationContext.xml</param-value>
二:自己写的监听器:在web.xml中配置
<listener>
<listener-class>写我们程序员自己写的监听器的位</listener-class>
</listener>
注:我们自己写的监听器要实现具体的监听器类,下面便是具体的监听器
监听器一般就是在项目启动时就运行
三:servlet规范中定义了多种类型的监听器
他们用于监听的事件源分别是下面的servletContext,HttpSession.ServletRequset这3 个域对象.
把多种类型的监听器分为三种类型。
* 1 监听3 个域对象创建和销毁的事件监听器.
* 2监听域对象中属性的增加和删除的事件监听器.
* 3监听绑定到httpSession域中的某个对象的状态的时间监听器.
servletRequestListener有两个方法:
* void requestDestroyed(ServletRequestEvent sre) 销毁
* void requestInitialized(ServletRequestEvent sre) 初始化创建
HttpSessionListener有两个方法
* void sessionCreated(HttpSessionEvent se) 创造Session
* void sessionDestroyed(HttpSessionEvent se) 销毁sessio监听、
ServletContextListener
* void contextDestroyed(ServletContextEvent sce) 销毁 contex
* void contextInitialized(ServletContextEvent sce) 创造context
B 3个类型对象域中增删改的监听器
* ServletContextAttributeLiatener //程序上下文属性监听器
* HttpSessionAttributeListener, //Session属性监听器
* ServletRequestAttributeListener .//requset上下文属性监听器
C 感知型监听器(2个);
监听自己何时被绑到Session上,何时解绑了,何时被钝化了,何时被活化了(序列话到某个存储设置中)
* 注意: 这种监听器不需要注册,某个javabean实现下面的接口后就可以监听何时被绑定,解绑,钝化,活化。
* HttpSessionBindingListener //实现该接口的类,能检测自己何时被HttpSession绑定和解绑.
* HttpSessionActivationListener //实现该接口的类,能检测到自己何时被HttpSession激活和钝化(要求javaBean必须是实现了serializable接口)
* 用处:
* 比如在web应用中,会存在会话,通常的做法是将当前登录的用户存放到session会话中,如何实现统计在线人数,如何显示当前登录的用户,如何提出某些也登录的用户,
* 就可以通过httpSessionAttributeListener监听器的attributeAdded方法。等等

浙公网安备 33010602011771号