利用Spring Boot框架做事件发布和监听

一、编写事件

1.编写事件类并集成spring boot 事件接口,提供访问事件参数属性

java 复制代码
public class PeriodicityRuleChangeEvent extends ApplicationEvent {

    private final JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO;

    public PeriodicityRuleChangeEvent(Object source, JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {
        super(source);
        this.jwpDeployWorkOrderRuleDTO = jwpDeployWorkOrderRuleDTO;
    }

    public JwpDeployWorkOrderRuleDTO getJwpDeployWorkOrderRuleDTO() {
        return jwpDeployWorkOrderRuleDTO;
    }

}

二、编写监听类(必须写明监听事件类型,重写监听到事件后,处理方法)

java 复制代码
@Component
public class PeriodicityRuleListener implements ApplicationListener<PeriodicityRuleChangeEvent> {

    @Autowired
    private PeriodicityCreateProcessServiceImpl periodicityCreateProcessServiceImpl;

    @Override
    public void onApplicationEvent(PeriodicityRuleChangeEvent periodicityRuleChangeEvent) {
        periodicityCreateProcessServiceImpl.addTask(periodicityRuleChangeEvent.getJwpDeployWorkOrderRuleDTO());
    }


}

三、发布事件

java 复制代码
@compnent
public class PeriodicityStartProcessService {

    @Autowired
    private ApplicationEventPublisher publisher;

    private void triggerEvent(JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {
        PeriodicityRuleChangeEvent periodicityRuleChangeEvent = new PeriodicityRuleChangeEvent(this, jwpDeployWorkOrderRuleDTO);
        publisher.publishEvent(periodicityRuleChangeEvent);

    }

}
相关推荐
Fan_web2 分钟前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常4 分钟前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
龙图:会赢的5 分钟前
[C语言]--编译和链接
c语言·开发语言
IT学长编程1 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇1 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
Jiaberrr1 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui
杨哥带你写代码1 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
XKSYA(小巢校长)2 小时前
NatGo我的世界联机篇
开发语言·php
Cons.W2 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
憧憬成为原神糕手2 小时前
c++_ 多态
开发语言·c++