spring 监听器 Listener

简介: 简介Spring的事件为Bean和Bean之间的消息传递提供支持。当一个对象处理完某种任务后,通知另外的对象进行某些处理,常用的场景有进行某些操作后发送通知,消息、邮件等情况。 Spring提供一些主要的事件监听ServletContextListener -- 监听servletConte...

简介

Spring的事件为Bean和Bean之间的消息传递提供支持。当一个对象处理完某种任务后,通知另外的对象进行某些处理,常用的场景有进行某些操作后发送通知,消息、邮件等情况。 

Spring提供一些主要的事件监听

ServletContextListener -- 监听servletContext对象的创建以及销毁

    contextInitialized(ServletContextEvent arg0)   -- 创建时执行
    contextDestroyed(ServletContextEvent arg0)  -- 销毁时执行

HttpSessionListener  -- 监听session对象的创建以及销毁

    sessionCreated(HttpSessionEvent se)   -- 创建时执行
    sessionDestroyed(HttpSessionEvent se) -- 销毁时执行

ServletRequestListener -- 监听request对象的创建以及销毁

    requestInitialized(ServletRequestEvent sre) -- 创建时执行
    requestDestroyed(ServletRequestEvent sre) -- 销毁时执行

ServletContextAttributeListener  -- 监听servletContext对象中属性的改变

    attributeAdded(ServletContextAttributeEvent event) -- 添加属性时执行
    attributeReplaced(ServletContextAttributeEvent event) -- 修改属性时执行
    attributeRemoved(ServletContextAttributeEvent event) -- 删除属性时执行

HttpSessionAttributeListener  --监听session对象中属性的改变

    attributeAdded(HttpSessionBindingEvent event) -- 添加属性时执行
    attributeReplaced(HttpSessionBindingEvent event) -- 修改属性时执行
    attributeRemoved(HttpSessionBindingEvent event) -- 删除属性时执行

ServletRequestAttributeListener  --监听request对象中属性的改变

    attributeAdded(ServletRequestAttributeEvent srae) -- 添加属性时执行
    attributeReplaced(ServletRequestAttributeEvent srae) -- 修改属性时执行
    attributeRemoved(ServletRequestAttributeEvent srae) -- 删除属性时执行

写一个session监听器用例

@Configuration
@WebListener
public class MyHttpSessionListener implements HttpSessionListener {

@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
    System.out.println("【监听器:Session】创建");
}

@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    System.out.println("【监听器:Session】 销毁");
}
}

自定义事件监听 

springboot中事件和事件监听器的父类分别是:ApplicationEvent和ApplicationListener。

自定义事件监听用例 

监听部分

@Component
public class MsgListener implements ApplicationListener<SendMsgEvent> {
@Override
public void onApplicationEvent(SendMsgEvent sendMsgEvent) {
    sendMsgEvent.output();
    System.out.println(sendMsgEvent.receiver + " MsgListener msg : " + sendMsgEvent.content );
}
}

事件部分

public class SendMsgEvent extends ApplicationEvent {

private static final long serialVersionID = 1L;

// 收件人
public String receiver;

// 收件内容
public String content;

public SendMsgEvent(Object source) {
    super(source);
}

public SendMsgEvent(Object source, String receiver, String content) {
    super(source);
    this.receiver = receiver;
    this.content = content;
}

public void output(){
    System.out.println("I had been sand a msg to " + this.receiver);
}
}

测试用例

@Autowired
ApplicationContext applicationContext;

@GetMapping("/createSession")
public void createSession(HttpServletRequest request) {
    HttpSession sessoin=request.getSession();//这就是session的创建
    sessoin.setAttribute("x","x");//给session添加属性属性
    applicationContext.publishEvent(new SendMsgEvent(source, receiver, content));
}

测试结果

QQ_20190121145421

目录
相关文章
|
5月前
|
Web App开发 监控 Java
|
2月前
|
缓存 Java 数据库
Spring Boot中使用监听器
系统的介绍了监听器原理,以及在 Spring Boot 中如何使用监听器,列举了监听器的三个常用的案例,有很好的实战意义。最后讲解了项目中如何自定义事件和监听器,并结合微服务中常见的场景,给出具体的代码模型,均能运用到实际项目中去,希望读者认真消化。
|
5月前
|
NoSQL Java Redis
Spring Boot 监听 Redis Key 失效事件实现定时任务
Spring Boot 监听 Redis Key 失效事件实现定时任务
122 0
|
Java Spring
spring boot中提供了一些监听方法,现在我需要在系统启动前完成一些操作。用什么方法实现或者注解?
spring boot中提供了一些监听方法,现在我需要在系统启动前完成一些操作。用什么方法实现或者注解?
|
4月前
|
监控 NoSQL Java
在 Spring Boot 中实现 Redis 的发布/订阅功能可以通过 RedisTemplate 和消息监听器来完成
在 Spring Boot 中实现 Redis 的发布/订阅功能可以通过 RedisTemplate 和消息监听器来完成
165 1
|
5月前
|
Java Spring
flowable 监听器无法获取spring bean ,自动注入bean为null,解决方案
flowable 监听器无法获取spring bean ,自动注入bean为null,解决方案
|
5月前
|
NoSQL Java Redis
Spring boot 实现监听 Redis key 失效事件
【2月更文挑战第2天】 Spring boot 实现监听 Redis key 失效事件
439 0
|
5月前
spring-state-machine监听器
spring-state-machine监听器
36 0
|
5月前
spring-state-machine监听器
spring-state-machine监听器
44 0
下一篇
无影云桌面