SpringBoot启动配置原理

Posted Young_Yang_Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot启动配置原理相关的知识,希望对你有一定的参考价值。

几个重要的事件回调机制

配置在META-INF/spring.factories

ApplicationContextInitializer

SpringApplicationRunListener

 

只需要放在ioc容器中

ApplicationRunner

CommandLineRunner

 

启动流程

1.创建SpringApplication对象

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

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

3、事件监听机制

配置在META-INF/spring.factories

ApplicationContextInitializer

SpringApplicationRunListener

 

只需要放在ioc容器中

ApplicationRunner

CommandLineRunner

以上是关于SpringBoot启动配置原理的主要内容,如果未能解决你的问题,请参考以下文章

springboot 嵌入式Servlet容器自动配置原理和容器启动原理

SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“(代码片段

SpringBoot自动配置注解原理解析

SpringBoot自动配置原理

springboot打包发布原理

Spring Boot启动原理解析