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

编程入门 行业动态 更新时间:2024-10-20 21:04:54

利用Spring Boot<a href=https://www.elefans.com/category/jswz/34/1770644.html style=框架做事件发布和监听"/>

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

一、编写事件

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

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;}}

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

@Component
public class PeriodicityRuleListener implements ApplicationListener<PeriodicityRuleChangeEvent> {@Autowiredprivate PeriodicityCreateProcessServiceImpl periodicityCreateProcessServiceImpl;@Overridepublic void onApplicationEvent(PeriodicityRuleChangeEvent periodicityRuleChangeEvent) {periodicityCreateProcessServiceImpl.addTask(periodicityRuleChangeEvent.getJwpDeployWorkOrderRuleDTO());}}

三、发布事件

@compnent
public class PeriodicityStartProcessService {@Autowiredprivate ApplicationEventPublisher publisher;private void triggerEvent(JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {PeriodicityRuleChangeEvent periodicityRuleChangeEvent = new PeriodicityRuleChangeEvent(this, jwpDeployWorkOrderRuleDTO);publisher.publishEvent(periodicityRuleChangeEvent);}}

更多推荐

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

本文发布于:2023-12-08 00:38:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1672116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   事件   Spring   Boot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!