利用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);

    }

}
相关推荐
茂桑3 分钟前
MVCC(多版本并发控制)
java·开发语言·数据库
customer0818 分钟前
【开源免费】基于SpringBoot+Vue.JS医疗报销系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
道不尽世间的沧桑20 分钟前
第9篇:插槽(Slots)的使用
前端·javascript·vue.js
bin915323 分钟前
DeepSeek 助力 Vue 开发:打造丝滑的滑块(Slider)
前端·javascript·vue.js·前端框架·ecmascript·deepseek
barcke27 分钟前
【深度解析】Java接入DeepSeek大模型:从零实现流式对话+多轮会话管理(完整项目实战) —— SpringBoot整合、API安全封装、性能优化全攻略
java·spring boot
zl97989939 分钟前
MybatisPlus-注解
java·spring·maven
uhakadotcom41 分钟前
最新发布的Tailwind CSS v4.0提供了什么新能力?
前端
thinkMoreAndDoMore1 小时前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python
杰九1 小时前
【环境配置】maven,mysql,node.js,vue的快速配置与上手
java·vue.js·spring boot·mysql·node.js·maven
GISer_Jing1 小时前
Node.js中如何修改全局变量的几种方式
前端·javascript·node.js