When an event is posted to the ApplicationContext provided by Spring and is detected by the listener, the corresponding processing method will be executed.
Event itself <br />Event is a custom class that needs to inherit ApplicationEvent provided by Spring.
@Datapublic class MyEvent extends ApplicationEvent { private String msg; public MyEvent(Object source, String msg) { super(source); this.msg = msg; }} Event listening
The basic method is to implement the ApplicationListener interface, customize a listener, implement onApplicationEvent() method, and then add it to ApplicationContext .
for example:
public class MyListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { System.out.print("Speaking MyEvent event"); } } ...// Add listener to the startup class of SpringBoot public static void main(String[] args) { SpringApplication application = new SpringApplication(MyApplication.class); application.addListeners(new MyListener()); application.run(args); } You can also use the annotation @ EventListener (recommended): the principle is to scan this annotation, create a listener and add it to ApplicationContext .
@Component@Slf4jpublic class MyEventHandler { @EventListener public void handleEvent(MyEvent event) { log.info("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Event release
It can be published through the context object publishing method ConfigurableApplicationContext::publishEvent() .
You can also implement the ApplicationEventPublisherAware interface to publish (recommended).
@Component@Slf4jpublic class EventService implements ApplicationEventPublisherAware { public ApplicationEventPublisher publisher; @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.publisher = applicationEventPublisher; } public String doEventWork(String msg) { log.info("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- msg); publisher.publishEvent(event); return "OK"; }} Test code
@SpringBootTest@RunWith(SpringRunner.class)public class EventServiceTest { @Autowired private EventService service; @Test public void eventTest() { String msg="Java Code"; service.doEventWork(msg); }} Notice
If there is an inheritance relationship between the two events, the subclass event will be listened to first, and then the parent class will be listened to after processing.
// MyEvent2 extends MyEvent@Component@Slf4jpublic class MyEventHandler { @EventListener public void handleEvent(MyEvent event) { log.info("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- log.info("-------------------------- Processing Event 2: {}", event.getMsg()); try { Thread.sleep(10 * 1000L); log.info("Event 2(10s) processing completed"); } catch (InterruptedException e) { e.printStackTrace(); } }}When I publish a subclass event MyEvent2, the log is as follows:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.