SpringBoot中ApplicationEvent的使用步骤详解
介绍
ApplicationEvent类似于MQ,是Spring提供的一种发布订阅模式的事件处理方式。相对于MQ,其局限在于只能在同一个Spring容器中使用。
使用步骤
封装消息
将要发送的内容,封装成一个bean,这个bean需要继承ApplicationEvent类。
package com.example.event;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;
/**
* @Description: 封装消息
* @author: zeal
* @date: 2024年04月09日 10:22
*/
@Setter
@Getter
@ToString
public class UserLoginEvent extends ApplicationEvent {
private Integer userId;
private String token;
public UserLoginEvent(Object source,Integer userId,String token) {
super(source);
this.userId=userId;
this.token=token;
}
}推送消息
推送消息时,注入ApplicationEventPublisher或ApplicationContext均可,调用publishEvent()方法。
package com.example.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description: 消息推送
* @author: zeal
* @date: 2024年04月09日 10:26
*/
@RestController
@RequestMapping("event")
public class UserLoginController {
@Autowired
private ApplicationContext applicationContext;
@RequestMapping("/push")
public void pushEvent(){
UserLoginEvent userLoginEvent=new UserLoginEvent(this,001,"zsaf");
applicationContext.publishEvent(userLoginEvent);
}
}监听消息
此步骤相当于MQ的消费者,实现ApplicatonListener类,通过泛型来设置消息类型。
package com.example.event;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @Description: 监听消息
* @author: zeal
* @date: 2024年04月09日 10:25
*/
@Component
public class UserLoginEventListener implements ApplicationListener<UserLoginEvent> {
@Override
public void onApplicationEvent(UserLoginEvent event) {
System.out.println("收到消息:"+event.toString());
}
}通过注解实现监听
package com.example.event;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* @Description: 监听消息
* @author: zeal
* @date: 2024年04月09日 10:25
*/
@Component
public class UserLoginEventListener{
@EventListener
public void onApplicationEvent(UserLoginEvent event) {
System.out.println("收到消息:"+event.toString());
}
}到此这篇关于SpringBoot中ApplicationEvent的用法的文章就介绍到这了,更多相关SpringBoot ApplicationEvent用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
spring @Validated 注解开发中使用group分组校验的实现
这篇文章主要介绍了spring @Validated 注解开发中使用group分组校验的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-05-05
spring注入在有常量的情况下使用@AllArgsConstructor操作
这篇文章主要介绍了spring注入在有常量的情况下使用@AllArgsConstructor操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09
Java使用多线程批次查询大量数据(Callable返回数据)方式
今天给大家分享Java使用多线程批次查询大量数据(Callable返回数据)方式,多线程有好几种方式,今天说的方式比较好,实现Callable<> 这种方式能返回查询的数据,加上Future异步获取方式,查询效率大大加快,感兴趣的朋友一起看看吧2023-11-11
SpringBoot项目如何把接口参数中的空白值替换为null值(推荐)
这篇文章主要介绍了SpringBoot项目如何把接口参数中的空白值替换为null值(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01
Spring Boot mybatis-config 和 log4j 输出sql 日志的方式
这篇文章主要介绍了Spring Boot mybatis-config 和 log4j 输出sql 日志的方式,本文通过实例图文相结合给大家介绍的非常详细,需要的朋友可以参考下2021-07-07
springboot HandlerIntercepter拦截器修改request body数据的操作
这篇文章主要介绍了springboot HandlerIntercepter拦截器修改request body数据的操作,具有很好的参考价值,希望对大家有所帮助。2021-06-06


最新评论