1.继承ApplicationEvent
public class TestEvent extends ApplicationEvent {
public TestEvent(Object source) {
super(source);
}
}
2.注解配置监听方法
@Component
public class TestListener {
@Async
@EventListener
public void whatEverMethodName(TestEvent testEvent) {
Object source = testEvent.getSource();
}
}
3.调用/触发事件监听
@RestController
public class TestImpl {
@Autowired
private ApplicationEventPublisher publisher;
@GetMapping("/test")
public void test() {
TestEvent testEvent = new TestEvent("Test_1");
publisher.publishEvent(testEvent);
}
}