Spring ApplicationEvent 使用

简介: Spring ApplicationEvent 使用

Spring ApplicationEvent 使用

ApplicationEvent为Spring的事件基类,可通过@EventListener或实现ApplicationListener接口进行监听

监听及触发事件

自定义事件

import org.springframework.context.ApplicationEvent;

public class CustomAnnotationEvent extends ApplicationEvent {
    private static final long serialVersionUID = -4180050946434009635L;
    /**
     * 类型
     */
    private String type;
    /**
     * 构造方法
     *
     * @param source 事件源
     * @param type   类型
     */
    public CustomAnnotationEvent(Object source, String type) {
        super(source);
        this.type = type;
    }
    /**
     * 获取类型
     *
     * @return 获取类型
     */
    public String getType() {
        return type;
    }
}
AI 代码解读

抛出事件

//spring注入
@Autowired
private ApplicationContext applicationContext;
...
CustomAnnotationEvent event = new CustomAnnotationEvent(this, "annotation");
applicationContext.publishEvent(event);
AI 代码解读

监听事件

@EventListener
public void listenCustomAnnotationEventAll(CustomAnnotationEvent event) {
    log.info("listenCustomAnnotationEventAll:{}", JSONUtil.toJsonStr(event));
}
AI 代码解读
监听事件及抛出事件的类需为spring管理的bean

其它

@EventListener(condition = "#event.type eq 'anycAnnotation' ")
@Async
public void listenCustomAnnotationAsyncEvent(CustomAnnotationEvent event) {
    log.info("listenCustomAnnotationEvent1:{}", JSONUtil.toJsonStr(event));
}
AI 代码解读
  • 异步事件:在方法上增加@Async注解则会将事件处理转为异步处理,异常及耗时不会影响抛出事件的方法,需在启动类中增加@EnableAsync开启此功能
  • 条件过滤:@EventListener注解中condition为SpEL表达式,可访问参数中的属性进行判断是否处理此事件
  • 同时监听多个事件:可使用@EventListene注解中classes条件扩充监听的事件
@EventListener(classes = { CustomAnnotationEvent.class, CustomAsyncErrorEvent.class,CustomAsyncEvent.class, CustomMetohEvent.class})
public void handleMultipleEvents(ApplicationEvent event) {
    log.info("handleMultipleEvents:{}", JSONUtil.toJsonStr(event));
}
AI 代码解读

标准事件

事件类 说明
ContextRefreshedEvent ApplicationContext初始化或刷新时发布
ContextStartedEvent 手动调用start()方法时发布
ContextStoppedEvent 手动调用stop()方法时发布
ContextClosedEvent ApplicationContext关闭时发布
另还有很多内置事件可通过查看ApplicationEvent子类来查看,例如SessionCreationEvent,KafkaEvent,RedisKeyspaceEvent等

参考资料

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#context-functionality-events

https://www.baeldung.com/spring-context-events

目录
打赏
0
0
0
0
891
分享
相关文章
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(中)
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(中)
[Java Framework] [Spring] Spring Event / 事件的使用 一: ApplicationEvent
[Java Framework] [Spring] Spring Event / 事件的使用 一: ApplicationEvent
123 0
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(上)
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(上)
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(上)
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(下)
小家Spring】从Spring中的(ApplicationEvent)事件驱动机制出发,聊聊【观察者模式】【监听者模式】【发布订阅模式】【消息队列MQ】【EventSourcing】...(下)
Spring(22)——ApplicationEvent
22 ApplicationEvent Spring允许我们在ApplicationContext中发布ApplicationEvent事件,然后对应的ApplicationListener可以用来监听对应的事件。
1560 0
详细介绍SpringBoot启动流程及配置类解析原理
通过对 Spring Boot 启动流程及配置类解析原理的深入分析,我们可以看到 Spring Boot 在启动时的灵活性和可扩展性。理解这些机制不仅有助于开发者更好地使用 Spring Boot 进行应用开发,还能够在面对问题时,迅速定位和解决问题。希望本文能为您在 Spring Boot 开发过程中提供有效的指导和帮助。
59 12
SpringBoot项目打包成war包
通过上述步骤,我们成功地将一个Spring Boot应用打包成WAR文件,并部署到外部的Tomcat服务器中。这种方式适用于需要与传统Servlet容器集成的场景。
33 8
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
352 17
Spring Boot 两种部署到服务器的方式
springboot自动配置原理
Spring Boot 自动配置原理:通过 `@EnableAutoConfiguration` 开启自动配置,扫描 `META-INF/spring.factories` 下的配置类,省去手动编写配置文件。使用 `@ConditionalXXX` 注解判断配置类是否生效,导入对应的 starter 后自动配置生效。通过 `@EnableConfigurationProperties` 加载配置属性,默认值与配置文件中的值结合使用。总结来说,Spring Boot 通过这些机制简化了开发配置流程,提升了开发效率。
84 17
springboot自动配置原理
SpringBoot集成Shiro权限+Jwt认证
本文主要描述如何快速基于SpringBoot 2.5.X版本集成Shiro+JWT框架,让大家快速实现无状态登陆和接口权限认证主体框架,具体业务细节未实现,大家按照实际项目补充。
107 11

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等