监听器也叫Listener,是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。
Listener接口分类:
监听ServletContext相关
1.1 ServletContextListener监听ServletContext对象
1.2 ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改监听HttpSession相关
2.1 HttpSessionListener监听Session对象
2.2 HttpSessionActivationListener监听HTTP会话的active和passivate情况,passivate是指非活动的session被写入持久设备(比如硬盘),active相反。
2.3 HttpSessionAttributeListener监听Session中的属性操作监听ServletRequest相关
3.1 ServletRequestListener监听Request对象
3.2 ServletRequestAttributeListener监听Requset中的属性操作
Spring监听器(ContextLoaderListener):
直接将applicationContext.xml(spring的配置文件)放到/WEB-INF下,只在web.xml中声明一个listener
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
作用:在启动Web 容器时,自动装配Spring applicationContext.xml 的配置信息。
因为它实现了ServletContextListener 这个接口,在web.xml 配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener 中关联了ContextLoader 这个类,所以整个加载配置过程由ContextLoader 来完成