springboot extension point

Key interface (extension point)

1. SpringApplicationRunListener (springboot application startup monitoring interface)

Explanation:

  1. When the run method is just executed
  2. When the environment is established
  3. When the context is established
  4. Context load configuration
  5. After the context refresh is completed, before the running method is executed
  6. After all the above steps are completed
  7. After failed to start

The implementation class used by springboot in the spring.factories file is EventPublishingRunListener

In this implementation class, after implementing the above interface, it is used to publish various events, such as:

  1. ApplicationStartingEvent
  2. ApplicationEnvironmentPreparedEvent
  3. ApplicationContextInitializedEvent
  4. ApplicationPreparedEvent
  5. ApplicationStartedEvent
  6. ApplicationReadyEvent
  7. ApplicationFailedEvent

 

2、ApplicationListener

Similar to the above. But this interface is something of the spring framework, which is used for the processing of ConfigurableApplicationContext context events.

https://blog.csdn.net/sumengnan/article/details/113634063

With the help of the SpringFactoriesLoader mechanism, the configuration in the custom META-INF/spring.factories file of the SpringBoot project is as follows:

3. ApplicationContextInitializer (application context initializer)

This interface is also something of the spring framework.

Before the refresh of the ApplicationContext of the ConfigurableApplicationContext type (or subtype), the ConfiurableApplicationContext instance (AnnotationConfigServletWebServerApplicationContext) is further set and processed.

The source code is as follows:

Other application context initializers configured in the spring.factories file are as follows:

 

4、CommandLineRunner或ApplicationRunner

Called after springboot is started. We can perform some content, such as: read configuration files, database connections, etc.

The source code is as follows:

It is possible to implement Orderedinterfaces to adjust the order of execution.

Note: The only difference between the CommandLineRunner or ApplicationRunner interface is that the type of parameter is different

CommandLineRunner is a parameter of type ApplicationArguments. ApplicationRunner is an array of String.

 

5. ServletContextInitialzer (servlet context initializer)

Used to initialize ServletContext. Related to our servlet, such as filters, servlet, session, Listen, etc. can all be operated

The implementation class is as follows:

When you are a servlet (web environment), the onStartup method of these implementation classes when tomcat starts

Will be called by ServletWebServerApplicationContext

application:

The famous DispatchServlet class is injected into the Servlet through this interface

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/113729252