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用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot 文件上传大小配置的方法

    springboot 文件上传大小配置的方法

    本篇文章主要介绍了springboot 文件上传大小配置的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Java基础教程之static五大应用场景

    Java基础教程之static五大应用场景

    这篇文章主要给大家介绍了关于Java基础教程之static五大应用场景的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • Elasticsearch查询Range Query语法示例

    Elasticsearch查询Range Query语法示例

    这篇文章主要为大家介绍了Elasticsearch查询Range Query语法示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • 详解如何使用Java8 Steam流对Map进行排序

    详解如何使用Java8 Steam流对Map进行排序

    这篇文章主要给大家详细介绍了如何使用Java8 Steam流对Map进行排序,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-01-01
  • Spring boot如何基于拦截器实现访问权限限制

    Spring boot如何基于拦截器实现访问权限限制

    这篇文章主要介绍了Spring boot如何基于拦截器实现访问权限限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • 完美解决单例设计模式中懒汉式线程安全的问题

    完美解决单例设计模式中懒汉式线程安全的问题

    下面小编就为大家带来一篇完美解决单例设计模式中懒汉式线程安全的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • java自定义类加载器如何实现类隔离

    java自定义类加载器如何实现类隔离

    这篇文章主要介绍了java自定义类加载器如何实现类隔离问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • springboot实现指定mybatis中mapper文件扫描路径

    springboot实现指定mybatis中mapper文件扫描路径

    这篇文章主要介绍了springboot实现指定mybatis中mapper文件扫描路径方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 使用@Order控制配置类/AOP/方法/字段的加载顺序详解

    使用@Order控制配置类/AOP/方法/字段的加载顺序详解

    这篇文章主要介绍了使用@Order控制配置类/AOP/方法/字段的加载顺序详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • CCF考试试题之门禁系统java解题代码

    CCF考试试题之门禁系统java解题代码

    这篇文章主要为大家详细介绍了CCF考试试题之门禁系统java解题代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01

最新评论