一、简介
Java的监听器,也是系统级别的监听。监听器随web应用的启动而启动。Java的监听器在c/s模式里面经常用到,它会对特定的事件产生产生一个处理。监听在很多模式下用到,比如说观察者模式,就是一个使用监听器来实现的,在比如统计网站的在线人数。
1.1 监听器分类
1) 按监听的对象划分,可以分为:
ServletContext对象监听器
HttpSession对象监听器
ServletRequest对象监听器
2)按监听的事件划分
对象自身的创建和销毁的监听器
对象中属性的创建和消除的监听器
session中的某个对象的状态变化的监听器
二、适用场景
springMVC监听器主要的作用就是spring容器启动的时候加载一些数据,最常用的功能就是开发权限系统的时候,当监听器启动的时候,从数据库加载权限url。
三、实现
一、ContextLoaderListener
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,web.xml配置这个监听器启动容器时,就会默认执行它实现的方法。在ContextLoader-Listener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。查看ContextLoaderServlet的API,它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口;ContextLoader创建的是XmlWebApplicationContext这样一个类,实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory。这样一来spring中的所有bean都由这个类来创建。如果在web.xml中不写任何参数配置信息,默认的路径是/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml;如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
contextConfigLocation
/WEB-INF/classes/applicationContext-*.xml
在里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。由此可见applicationContext.xml的文件位置就可以有两种默认实现:第一种:直接将之放到/WEB-INF下,之后在web.xml中声明一个listener;第二种:将之放到classpath下,但是此时要在web.xml中加入,用它来指明你的applicationContext.xml的位置以供web容器来加载。按照Struts2 整合spring的官方给出的档案,写成:
contextConfigLocation
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
自定义监听器的配置
1配置自定义的监听器需要配置spring容器监听器,因为自定期监听器需要从spring容器中拿取数据,并且自定义监听器的配置文件位于spring监听器之后
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Device Manage Web Application
testFilter
com.my.dm.filter.TestFilter
demo
112.2.36
testFilter
/*
charsetEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
charsetEncoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring-mvc.xml
1
springmvc
/
contextConfigLocation
classpath:config/beans.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
org.springframework.web.util.IntrospectorCleanupListener
/welcome.jsp
404
/WEB-INF/error/404.jsp
500
/WEB-INF/error/500.jsp
com.my.dm.listener.TestListener
2,springMC自定义监听器需要实现ServletContextListener接口
package com.my.dm.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.my.dm.service.DeviceService;
public class TestListener implements ServletContextListener {
private Logger logger = LogManager.getLogger(TestListener.class);
/**
* 当容器启动完成之后
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
DeviceService deviceService = ac.getBean(DeviceService.class);
logger.error("This is test for TestListener!");
logger.error(deviceService.getDeviceByNameIp("", "4.154.135.74").get(0));
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
参考:
https://www.jianshu.com/p/82ae825b849b
https://www.cnblogs.com/lukelook/p/11079113.html#t2