In business development, listeners are often used. For example, when events of the same type are triggered in multiple scenarios, it is more suitable to use listeners. There are several forms of using listeners in spring boot, which are slightly different
Customize the start of SpringApplication
SpringApplication springApplication = new SpringApplication(Application.class); springApplication.addListeners(new ApplicationListenerTest()); springApplication.run(args);
Customize in META-INF/spring.factories
org.springframework.context.ApplicationListener=\ com.dragon.myapplication.mylistener.ApplicationListenerTest
Use @ Component annotation to inject
@Component public class ApplicationListenerTest implements ApplicationListener<ApplicationEvent> { public void onApplicationEvent(ApplicationEvent applicationEvent) { if(applicationEvent instanceof ApplicationStartingEvent){ System.out.println("Event superclass:ApplicationStartingEvent:"+ JSON.toJSONString(applicationEvent)); } if(applicationEvent instanceof ApplicationEnvironmentPreparedEvent){ System.out.println("Event superclass:ApplicationEnvironmentPreparedEvent:"+ JSON.toJSONString(applicationEvent)); } if(applicationEvent instanceof ApplicationContextInitializedEvent){ ApplicationContextInitializedEvent event = (ApplicationContextInitializedEvent)applicationEvent; System.out.println("Event superclass:ApplicationContextInitializedEvent"+event.getApplicationContext()); } if(applicationEvent instanceof ApplicationPreparedEvent){ ApplicationPreparedEvent event = (ApplicationPreparedEvent)applicationEvent; System.out.println("Event superclass:ApplicationPreparedEvent"+event.getApplicationContext()); } if(applicationEvent instanceof ApplicationStartedEvent){ ApplicationStartedEvent event = (ApplicationStartedEvent)applicationEvent; event.getApplicationContext().publishEvent(new MyApplicationEvent(event.getSource(),new Order())); System.out.println("Event superclass:ApplicationStartedEvent"+event.getApplicationContext()); } if(applicationEvent instanceof ApplicationReadyEvent){ ApplicationReadyEvent event = (ApplicationReadyEvent)applicationEvent; System.out.println("Event superclass:ApplicationReadyEvent"+event.getApplicationContext()); } if(applicationEvent instanceof ApplicationFailedEvent){ ApplicationFailedEvent event = (ApplicationFailedEvent)applicationEvent; System.out.println("Event superclass:ApplicationFailedEvent"+event.getApplicationContext()); } } }
Use other methods, such as @ Bean
What's the difference between these methods? It's mainly about the results of event monitoring. In general, business development, you only care about customized events. So @ Component is recommended. If you are interested in spring boot framework events, you need to use the first two methods. Because if you use annotation, some things happen when the container is not started The province has sent out the event, which leads to the failure of getting things. Another point is that if you get the event of the framework, you can get the container from the event, that is, you can get the container, and then you can do whatever you want