幾個重要的事件回調機制java
配置在META-INF/spring.factoriesweb
ApplicationContextInitializerspring
SpringApplicationRunListenerspringboot
只須要放在ioc容器中app
ApplicationRunnerless
CommandLineRunneride
啓動流程:ui
initialize(sources); private void initialize(Object[] sources) { //保存主配置類 if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources)); } //判斷當前是否一個web應用 this.webEnvironment = deduceWebEnvironment(); //從類路徑下找到META-INF/spring.factories配置的全部ApplicationContextInitializer;而後保存起來 setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); //從類路徑下找到ETA-INF/spring.factories配置的全部ApplicationListener setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); //從多個配置類中找到有main方法的主配置類 this.mainApplicationClass = deduceMainApplicationClass(); }
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHeadlessProperty(); //獲取SpringApplicationRunListeners;從類路徑下META-INF/spring.factories SpringApplicationRunListeners listeners = getRunListeners(args); //回調全部的獲取SpringApplicationRunListener.starting()方法 listeners.starting(); try { //封裝命令行參數 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //準備環境 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); //建立環境完成後回調SpringApplicationRunListener.environmentPrepared();表示環境準備完成 Banner printedBanner = printBanner(environment); //建立ApplicationContext;決定建立web的ioc仍是普通的ioc context = createApplicationContext(); analyzers = new FailureAnalyzers(context); //準備上下文環境;將environment保存到ioc中;並且applyInitializers(); //applyInitializers():回調以前保存的全部的ApplicationContextInitializer的initialize方法 //回調全部的SpringApplicationRunListener的contextPrepared(); // prepareContext(context, environment, listeners, applicationArguments, printedBanner); //prepareContext運行完成之後回調全部的SpringApplicationRunListener的contextLoaded(); //s刷新容器;ioc容器初始化(若是是web應用還會建立嵌入式的Tomcat);Spring註解版 //掃描,建立,加載全部組件的地方;(配置類,組件,自動配置) refreshContext(context); //從ioc容器中獲取全部的ApplicationRunner和CommandLineRunner進行回調 //ApplicationRunner先回調,CommandLineRunner再回調 afterRefresh(context, applicationArguments); //全部的SpringApplicationRunListener回調finished方法 listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } //整個SpringBoot應用啓動完成之後返回啓動的ioc容器; return context; } catch (Throwable ex) { handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); } }
配置在META-INF/spring.factoriesthis
ApplicationContextInitializerspa
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext applicationContext) { System.out.println("ApplicationContextInitializer...initialize..."+applicationContext); } }
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..."); } }
配置(META-INF/spring.factories)
org.springframework.context.ApplicationContextInitializer=\ com.atguigu.springboot.listener.HelloApplicationContextInitializer org.springframework.boot.SpringApplicationRunListener=\ com.atguigu.springboot.listener.HelloSpringApplicationRunListener
只須要放在ioc容器中
ApplicationRunner
@Component public class HelloApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner...run...."); } }
CommandLineRunner
@Component public class HelloCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner...run..."+ Arrays.asList(args)); } }