1系统监听器
和过滤器差不多就不详细说了
类 ListenerConfig
@Configuration
public class ListenerConfig {
@Bean
public EventListener demoListener(){
return new DemoListener();
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(demoListener());
return servletListenerRegistrationBean;
}
}
类 DemoListener
/**
* 系统的监听器
* 1.ServletContextListener -- 监听servletContext对象的创建以及销毁
* 2.HttpSessionListener -- 监听session对象的创建以及销毁
* 3.ServletRequestListener -- 监听request对象的创建以及销毁
* 4.ServletContextAttributeListener -- 监听servletContext对象中属性的改变
* 5.HttpSessionAttributeListener --监听session对象中属性的改变
* 6.ServletRequestAttributeListener --监听request对象中属性的改变
* 7.自定义监听器,需要配置事件,监听器类
*/
@Slf4j
public class DemoListener implements ServletContextListener, HttpSessionAttributeListener {
/**
* * Notification that the web application initialization process is starting.
* All ServletContextListeners are notified of context initialization before
* any filter or servlet in the web application is initialized.
* The default implementation is a NO-OP.
*
* @param sce Information about the ServletContext that was initialized
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("上下文初始化监听{}",sce);
}
/**
* * Notification that the servlet context is about to be shut down. All
* servlets and filters have been destroyed before any
* ServletContextListeners are notified of context destruction.
* The default implementation is a NO-OP.
*
* @param sce Information about the ServletContext that was destroyed
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("上下文销毁{}",sce);
}
/**
* Notification that an attribute has been added to a session. Called after
* the attribute is added.
* The default implementation is a NO-OP.
*
* @param se Information about the added attribute
*/
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
log.info("session添加{}",se.getValue());
}
/**
* Notification that an attribute has been removed from a session. Called
* after the attribute is removed.
* The default implementation is a NO-OP.
*
* @param se Information about the removed attribute
*/
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
log.info("session删除{}",se.getValue());
}
/**
* Notification that an attribute has been replaced in a session. Called
* after the attribute is replaced.
* The default implementation is a NO-OP.
*
* @param se Information about the replaced attribute
*/
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
log.info("session替换{}",se.getValue());
}
}
然后操作会有监听,
2自定义监听器
1.创建事件
/**
* 自定义
* 监听事件
*/
public class DemoEvent extends ApplicationEvent {
public String mas;
public DemoEvent(Object source,String mas) {
super(source);
this.mas=mas;
}
/**
* Create a new {@code ApplicationEvent}.
*
* @param source the object on which the event initially occurred or with
* which the event is associated (never {@code null})
*/
public DemoEvent(Object source) {
super(source);
}
}
2注入自定义监听器
/**
* 注入自定义监听器
*/
@Slf4j
@Component
public class CustomizeListener implements ApplicationListener<DemoEvent> {
/**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(DemoEvent event) {
log.info("DemoListener:"+event.mas);
}
}
3. 发布事件
@Autowired
private ApplicationContext applicationContext;
/**
* 自定义监听器
*/
@GetMapping("listener")
public void listener() {
applicationContext.publishEvent(new DemoEvent(this,"哈哈"));
}