In the startup configuration SpringBoot, there are several important callback mechanism ApplicationContextInitializer, SpringApplicationRunListener, ApplicationRunner, CommandLineRunner, the first two are configured in a META-INF / spring.factories, the latter two are only placed ioc container.
the initialize (Sources); Private void the initialize (Object [] Sources) { // save the master configuration type IF (Sources =! null && sources.length> 0 ) { the this .sources.addAll (Arrays.asList (Sources)); } // determines whether a current web application the this .webEnvironment = deduceWebEnvironment (); // find all ApplicationContextInitializer META-INF / spring.factories configuration from the classpath; and saved ((Collection) getSpringFactoriesInstances (setInitializers . ApplicationContextInitializer class )) ; // find ETA-INF from the classpath / spring.factories all configured ApplicationListener setListeners ((Collection) getSpringFactoriesInstances (the ApplicationListener. class )); // find the main method of the main class from the plurality of configuration classes arranged in the this .mainApplicationClass = deduceMainApplicationClass (); }
Debug mode is turned on to view Initializer, Listener
2, run run method
public ConfigurableApplicationContext RUN (String ... args) { the StopWatch StopWatch = new new the StopWatch (); stopWatch.start (); ConfigurableApplicationContext context = null ; FailureAnalyzers Analyzers = null ; configureHeadlessProperty (); // Get SpringApplicationRunListeners; from the class path META- INF / spring.factories SpringApplicationRunListeners the Listeners = getRunListeners (args); // callback all acquired SpringApplicationRunListener.starting () method listeners.starting (); the try { // package the command line parameters ApplicationArguments = ApplicationArguments new new DefaultApplicationArguments ( args); // Prepare the environment ConfigurableEnvironment Environment = prepareEnvironment (in the Listeners, applicationArguments); // create the environment after completion callback SpringApplicationRunListener.environmentPrepared (); denote environment ready Banner printedBanner = printBanner (Environment); // create ApplicationContext; decided to create a web or ordinary ioc ioc context = createApplicationContext (); Analyzers = new new FailureAnalyzers (context); //Preparation context; the environment is saved to the ioc; and applyInitializers (); // applyInitializers (): initialize callback method before all ApplicationContextInitializer saved // callback contextPrepared all SpringApplicationRunListener of (); // prepareContext (context, environment , in the Listeners, applicationArguments, printedBanner); // after completion callback contextLoaded prepareContext run all SpringApplicationRunListener of (); // S refresh container; ioc container initialization (if a web application will create an embedded Tomcat); Spring annotated version / / scan, created, where all the components of the load; (configuration classes, components, automatic configuration) refreshContext (context); // Get all callbacks from CommandLineRunner ApplicationRunner and ioc container // ApplicationRunner first callback, then the callback CommandLineRunner AfterRefresh (context, applicationArguments); // All finished SpringApplicationRunListener callback method listeners.finished (context, null ); stopWatch.stop (); IF ( the this .logStartupInfo) { new new StartupInfoLogger ( the this .mainApplicationClass) .logStarted (getApplicationLog () , StopWatch); } // entire SpringBoot application returns to start after the completion of start ioc container; return context; } the catch (the Throwable EX) { handleRunFailure (context, the Listeners, Analyzers, EX); the throw new new IllegalStateException (EX); } }
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext applicationContext) { System.out.println("ApplicationContextInitializer...initialize..."+applicationContext); } }
2.SpringApplicationRunListener
public class HelloSpringApplicationRunListener implements SpringApplicationRunListener { //必须有的构造器 public HelloSpringApplicationRunListener(SpringApplication application, String[] args){ } @Override public void starting() { System.out.println("SpringApplicationRunListener...starting..."); } @Override public void environmentPrepared(ConfigurableEnvironment environment) { Object o = environment.getSystemProperties().get("os.name"); System.out.println("SpringApplicationRunListener...environmentPrepared.."+o); } @Override public void contextPrepared(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener...contextPrepared..."); } @Override public void contextLoaded(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener...contextLoaded..."); } @Override public void finished(ConfigurableApplicationContext context, Throwable exception) { System.out.println("SpringApplicationRunListener...finished..."); } }
Configuration (META-INF / spring.factories)
org.springframework.context.ApplicationContextInitializer=\
com.atguigu.springboot.listener.HelloApplicationContextInitializer
org.springframework.boot.SpringApplicationRunListener=\
com.atguigu.springboot.listener.HelloSpringApplicationRunListener
3.ApplicationRunner
Ioc container only needs to be placed, via annotations -> @Component
@Component public class HelloApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner...run...."); } }
4.CommandLineRunner
Ioc container only needs to be placed, via annotations -> @ Component
@Component public class HelloCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner...run..."+ Arrays.asList(args)); } }
Run the program can be observed in order SpringBoot start loading, and the implementation process.