1、发布事件

@Data
public class CustomEvent extends ApplicationEvent implements Serializable {
   private Boolean  ignore = Boolean.FALSE;  
   ...
}

 

@Resource
private ApplicationEventPublisher   publisher;

CustomEvent customEvent = new CustomEvent();
customEvent.setIgnore(true);
publisher.publishEvent(customEvent);

  

2、处理事件

@Component
public class MyEventListener {
    @Async
    @EventListener(condition = "#event.ignore")
    public void handleCustomEvent(CustomEvent event) {
        ...
    }
}

注意: 

  1. 首先它是一个spring组件
  2. 必须声明它是异步的
  3. 声明它是一个监听器
  4. 方法中传递的参数表明它是监听哪个事件
  5. 从监听事件中取参         

 

相关文章:

  • 2022-12-23
  • 2021-07-05
  • 1970-01-01
  • 2021-06-02
  • 2021-09-28
  • 2021-09-22
  • 2021-09-24
  • 2023-02-05
猜你喜欢
  • 2022-12-23
  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
  • 1970-01-01
  • 2021-10-19
  • 2022-01-02
相关资源
相似解决方案