角色介绍
事件主体
ApplicationEvent
生产者
ApplicationContext
消费者
ApplicationListener
使用
1、创建事件主体,继承ApplicationEvent
@Data
public class TestEvent extends ApplicationEvent{
private int id
;
private String name
;
}
2、发送事件,发送器无需定义
@Autowired
private ApplicationContext applicationContext
;
public void publish() {
TestEvent event
= new TestEvent();
applicationContext
.publishEvent(event
);
}
3、监听事件 创建监听器 继承ApplicationListener
@Component
@Slf4j
public class ArcticRefundNotifyListener implements ApplicationListener<TestEvent> {
@Async
@Override
public void onApplicationEvent(TestEvent testEvent
) {
}
}
作用
用于本地解耦,