springboot 运行方法:
1.实例化SpringApplication对象,然后调用run方法。
运行流程:
1.判断是否是web环境
2.加载所有classpath下面的META-INF/spring.factories 的 ApplicationContextInitializer类
3.加载所有classpath下面的META-INF/spring.factories 的ApplicationListener类
4.指断main方法所在的类
5.开始执行run方法
6.设置 java.awt.headless系统变量 System.setProperty("java.awt.headless", System.getProperty("java.awt.headless", Boolean.toString(this.headless)));
7.加载所有classpath下面的META-INF/spring.factories 的SpringApplicationListener类(主要再不同得时间点,发送事件通知)
8.执行所有SpringApplicationListener的start方法
9.实例化ApplicationArguments
10.创建Environment,根据不同的环境创建不同的Environment
11.配置 Environment,主要把mian方法参数配置到Environment
this.configureEnvironment(environment, applicationArguments.getSourceArgs());
12.执行所有SpringApplicationRunListeners的environmentPrepared方法
13. 如果不是web环境,但是是web的environment,则把这个web得environment,转换成StandardEnvironment
14.Banner打印环境 environment
15初始化ApplicationContext,如果是web环境则初始化org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext ,否则org.springframework.context.annotation.AnnotationConfigApplicationContext
16 .如果beanNameGenerator不为空,就把beanNameGenerator注入到spring容器(主要是一些方法的回调 , 比如我们通过app.setBeanNameGenerator();设置)
17.回调所有的ApplicationContextInitializer方法
18.执行所有SpringApplicationRunListeners的contextPrepared方法
19.一次方springr容器注入ApplicationArguments、Banner
20.加载所有的源到context
21.执行所有SpringApplicationRunListeners的contextLoaded方法
22.执行context的refresh方法,并且调用context得registerShutdownHook方法,到这里spring容器已经初始化完了
23回调,获取容器中所有的ApplicationRunner、CommandLineRunner接口,然后排序
24.执行所有SpringApplicationRunListeners的finished方法
2.直接调用SpringApplication静态run方法。
这一种方法,其实是将第一种方法封装。
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
return (new SpringApplication(sources)).run(args);
}