几个重要的事件回调机制,配置在META-INF/spring.factories中:
ApplicationContextInitializerSpringApplicationRunListener

只需要放在ioc容器中:ApplicationRunnerCommandLineRunner

启动流程

1、创建SpringApplication对象

  1. private void initialize(Object[] sources) {
  2. //保存主配置类
  3. if (sources != null && sources.length > 0) {
  4. this.sources.addAll(Arrays.asList(sources));
  5. }
  6. //判断当前是否一个web应用
  7. this.webEnvironment = deduceWebEnvironment();
  8. //从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
  9. setInitializers((Collection) getSpringFactoriesInstances(
  10. ApplicationContextInitializer.class));
  11. //从类路径下找到ETA-INF/spring.factories配置的所有ApplicationListener
  12. setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  13. //从多个配置类中找到有main方法的主配置类
  14. this.mainApplicationClass = deduceMainApplicationClass();
  15. }

搜狗截图20180306145727.png搜狗截图20180306145855.png
2、运行run方法

  1. public ConfigurableApplicationContext run(String... args) {
  2. StopWatch stopWatch = new StopWatch();
  3. stopWatch.start();
  4. ConfigurableApplicationContext context = null;
  5. FailureAnalyzers analyzers = null;
  6. configureHeadlessProperty();
  7. //获取SpringApplicationRunListeners;从类路径下META-INF/spring.factories
  8. SpringApplicationRunListeners listeners = getRunListeners(args);
  9. //回调所有的获取SpringApplicationRunListener.starting()方法
  10. listeners.starting();
  11. try {
  12. //封装命令行参数
  13. ApplicationArguments applicationArguments = new DefaultApplicationArguments(
  14. args);
  15. //准备环境
  16. ConfigurableEnvironment environment = prepareEnvironment(listeners,
  17. applicationArguments);
  18. //创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成
  19. Banner printedBanner = printBanner(environment);
  20. //创建ApplicationContext;决定创建web的ioc还是普通的ioc
  21. context = createApplicationContext();
  22. analyzers = new FailureAnalyzers(context);
  23. //准备上下文环境;将environment保存到ioc中;而且applyInitializers();
  24. //applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
  25. //回调所有的SpringApplicationRunListener的contextPrepared();
  26. //
  27. prepareContext(context, environment, listeners, applicationArguments,
  28. printedBanner);
  29. //prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded();
  30. //s刷新容器;ioc容器初始化(如果是web应用还会创建嵌入式的Tomcat);Spring注解版
  31. //扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
  32. refreshContext(context);
  33. //从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
  34. //ApplicationRunner先回调,CommandLineRunner再回调
  35. afterRefresh(context, applicationArguments);
  36. //所有的SpringApplicationRunListener回调finished方法
  37. listeners.finished(context, null);
  38. stopWatch.stop();
  39. if (this.logStartupInfo) {
  40. new StartupInfoLogger(this.mainApplicationClass)
  41. .logStarted(getApplicationLog(), stopWatch);
  42. }
  43. //整个SpringBoot应用启动完成以后返回启动的ioc容器;
  44. return context;
  45. }
  46. catch (Throwable ex) {
  47. handleRunFailure(context, listeners, analyzers, ex);
  48. throw new IllegalStateException(ex);
  49. }
  50. }

3、事件监听机制
配置在META-INF/spring.factories

ApplicationContextInitializer

  1. public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
  2. @Override
  3. public void initialize(ConfigurableApplicationContext applicationContext) {
  4. System.out.println("ApplicationContextInitializer...initialize..."+applicationContext);
  5. }
  6. }

SpringApplicationRunListener

  1. public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
  2. //必须有的构造器
  3. public HelloSpringApplicationRunListener(SpringApplication application, String[] args){
  4. }
  5. @Override
  6. public void starting() {
  7. System.out.println("SpringApplicationRunListener...starting...");
  8. }
  9. @Override
  10. public void environmentPrepared(ConfigurableEnvironment environment) {
  11. Object o = environment.getSystemProperties().get("os.name");
  12. System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
  13. }
  14. @Override
  15. public void contextPrepared(ConfigurableApplicationContext context) {
  16. System.out.println("SpringApplicationRunListener...contextPrepared...");
  17. }
  18. @Override
  19. public void contextLoaded(ConfigurableApplicationContext context) {
  20. System.out.println("SpringApplicationRunListener...contextLoaded...");
  21. }
  22. @Override
  23. public void finished(ConfigurableApplicationContext context, Throwable exception) {
  24. System.out.println("SpringApplicationRunListener...finished...");
  25. }
  26. }


ApplicationRunner

  1. @Component
  2. public class HelloApplicationRunner implements ApplicationRunner {
  3. @Override
  4. public void run(ApplicationArguments args) throws Exception {
  5. System.out.println("ApplicationRunner...run....");
  6. }
  7. }

CommandLineRunner

  1. @Component
  2. public class HelloCommandLineRunner implements CommandLineRunner {
  3. @Override
  4. public void run(String... args) throws Exception {
  5. System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));
  6. }
  7. }