introduce

elements

  • The event
  • The listener
  • Radio apparatus
  • triggering

Sequence of events

Listener registration

Listener event triggering mechanism

Gets a list of listeners

Universal trigger condition

SpringApplicationRunListener

From the name we can know that it is a listener, we find that throughout the launch process, it is used in the entire launch process to receive different enforcement point event notification listener, SpringApplicationRunListener interface specifies SpringBoot lifecycle, The corresponding events are broadcast at each lifecycle, calling the actual ApplicationListener class.

Public interface SpringApplicationRunListener {/ / just void when performing a run method started (); // Void environmentPrepared(ConfigurableEnvironment); / / the context to establish good void contextPrepared (ConfigurableApplicationContext context). / / the context when loading configuration void contextLoaded (ConfigurableApplicationContext context). / / context refresh, after the completion of the run method before the execution of the void finished (ConfigurableApplicationContext context, Throwable exception). }Copy the code

It defines five steps:

  1. Started (Executed immediately when the run method is executed; The corresponding event type is ApplicationStartedEvent) : notifies the listener that SpringBoot has started execution
  2. EnvironmentPrepared (called before ApplicationContext is created and when the environment information is ready; On the type of event is ApplicationEnvironmentPreparedEvent) : notification listener, the Environment is prepared to do
  3. ContextPrepared (ApplicationContext is created and called once before the source is loaded; No specific event) : Notifies the listener that ApplicationContext has been created and initialized
  4. ContextLoaded (called after ApplicationContext is created and loaded and before refresh; The event type is ApplicationPreparedEvent) : notifies the listener that ApplicationContext has completed the IoC configuration value
  5. Finished (called before the run method ends; The corresponding event type is ApplicationReadyEvent or ApplicationFailedEvent) : notifies the listener that SpringBoot is complete

Implementation of a listener

Implement the ApplicationListener interface to listen for a single event

@order (1) public class FirstListener implements ApplicationListener<ApplicationStartedEvent> {@override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("FirstListener is run"); }}Copy the code

Implement SmartApplicationListener to listen for various events

@Order(4) public class FourthListener implements SmartApplicationListener {/** * Custom event supported actions * Running the program can see that the console prints twice FourthListener is run because we define two events: * @param eventType * @return */ @Override Public Boolean supportsEventType(Class<?  extends ApplicationEvent> eventType) { return ApplicationStartedEvent.class.isAssignableFrom(eventType) || ApplicationReadyEvent.class.isAssignableFrom(eventType); } @override public void onApplicationEvent(ApplicationEvent event) { System.out.println("FourthListener is run"); }}Copy the code

How listeners are registered

Method 1: Spring. factories configuration

# registered custom listener org. Springframework. Context. ApplicationListener = com. Ecit. Learn. The listener. FirstListenerCopy the code

Method 2: Enable class addition

@SpringBootApplication @MapperScan("com.ecit.learn.mapper") public class SpringbootLearnApplication { public static void  main(String[] args) { SpringApplication springApplication = new SpringApplication(SpringbootLearnApplication.class); / / add initialization starter springApplication. AddInitializers (new SecondInitializer ()); / / add custom listener springApplication. AddListeners (new SecondListener ()); springApplication.run(args); }}Copy the code

Method 3: Configure a configuration file

context.listener.classes=com.ecit.learn.listener.ThirdListener
Copy the code