SpringBoot中ApplicationEvent的使用步骤详解

 更新时间:2024年04月13日 10:57:57   作者:南瓜小米粥、  
ApplicationEvent类似于MQ,是Spring提供的一种发布订阅模式的事件处理方式,本文给大家介绍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 Boot项目的内嵌容器的理由

    选择Spring Boot项目的内嵌容器的理由

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。这篇文章主要介绍了选择Spring Boot项目的内嵌容器,需要的朋友可以参考下
    2017-11-11
  • spring @Validated 注解开发中使用group分组校验的实现

    spring @Validated 注解开发中使用group分组校验的实现

    这篇文章主要介绍了spring @Validated 注解开发中使用group分组校验的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Java返回分页结果集的封装代码实例

    Java返回分页结果集的封装代码实例

    这篇文章主要介绍了java返回分页结果集的封装代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • spring注入在有常量的情况下使用@AllArgsConstructor操作

    spring注入在有常量的情况下使用@AllArgsConstructor操作

    这篇文章主要介绍了spring注入在有常量的情况下使用@AllArgsConstructor操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java使用多线程批次查询大量数据(Callable返回数据)方式

    Java使用多线程批次查询大量数据(Callable返回数据)方式

    今天给大家分享Java使用多线程批次查询大量数据(Callable返回数据)方式,多线程有好几种方式,今天说的方式比较好,实现Callable<> 这种方式能返回查询的数据,加上Future异步获取方式,查询效率大大加快,感兴趣的朋友一起看看吧
    2023-11-11
  • SpringBoot项目如何把接口参数中的空白值替换为null值(推荐)

    SpringBoot项目如何把接口参数中的空白值替换为null值(推荐)

    这篇文章主要介绍了SpringBoot项目如何把接口参数中的空白值替换为null值(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Spring Boot mybatis-config 和 log4j 输出sql 日志的方式

    Spring Boot mybatis-config 和 log4j 输出sql 日志的方式

    这篇文章主要介绍了Spring Boot mybatis-config 和 log4j 输出sql 日志的方式,本文通过实例图文相结合给大家介绍的非常详细,需要的朋友可以参考下
    2021-07-07
  • Java的Shiro框架认证流程详解

    Java的Shiro框架认证流程详解

    这篇文章主要介绍了Java的Shiro框架认证流程详解,Shiro 是一个功能强大和易于使用的安全框架,为开发人员提供一个直观而全面的解决方案的认证,授权,加密,会话管理四大功能,需要的朋友可以参考下
    2024-01-01
  • Java中的Kafka消费者详解

    Java中的Kafka消费者详解

    这篇文章主要介绍了Java中的Kafka消费者详解,Kafka是一个分布式流行消息系统,通常用于大规模数据处理和实时数据流应用程序,它具有高吞吐量、可扩展性和容错性的特点,需要的朋友可以参考下
    2023-09-09
  • springboot HandlerIntercepter拦截器修改request body数据的操作

    springboot HandlerIntercepter拦截器修改request body数据的操作

    这篇文章主要介绍了springboot HandlerIntercepter拦截器修改request body数据的操作,具有很好的参考价值,希望对大家有所帮助。
    2021-06-06

最新评论