一、SpringApplication启动原理
SpringApplication.run(主程序类) 启动分2步
1 第一步:创建SpringApplication对象 并进行初始化initialize
- 判断是否web应用
- 加载并保存所有ApplicationContextInitializer(META-INF/spring.factories),
- 加载并保存所有ApplicationListener(从这个路径下找META-INF/spring.factories)
4)获取到主程序类
2 第二部执行 run()方法
1)获取所有监听器SpringApplicationRunListener(META-INF/spring.factories)并回调starting()方法
2)创建ApplicationArguments并进行命令行参数封装
3)准备环境&回调所有监听器(SpringApplicationRunListener)的environmentPrepared()方法
4)打印banner图标信息(springboot)
5 )创建ioc容器对象ApplicationContext
根据不同情况创建不同环境容器
a)AnnotationConfigEmbeddedWebApplicationContext(web环境容器)
b ) AnnotationConfigApplicationContext(普通环境容器)
6)准备上下文 执行 prepareContext()方法
a) 将environment保存到ioc中
b ) 调用applyInitializers():回调之前保存的所有ApplicationContextInitializer的initialize方法
c)回调所有的SpringApplicationRunListener的contextPrepared();
7)刷新容器 refreshContext():ioc初始化,扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
8)afterRefresh()//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
源码如下
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.factories
ApplicationContextInitializer
SpringApplicationRunListener
只需要放在ioc容器中
ApplicationRunner
CommandLineRunne
二、SpringApplication Demo 启动流程验证
1)新建springboot工程
2)创建HelloApplicationContextInitializer 实现ApplicationContextInitializer 接口
public class HelloApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
System.out.println("ApplicationContextInitializer...init.."+configurableApplicationContext);
}
}
3)创建HelloSpringApplicationRunListener 实现SpringApplicationRunListener 接口
public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
public HelloSpringApplicationRunListener(SpringApplication application,String[] args){
}
@Override
public void starting() {
System.out.println("starting....");
}
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
System.out.println("starting....");
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("starting...."+context);
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("starting...."+context);
}
@Override
public void started(ConfigurableApplicationContext context) {
System.out.println("starting...."+context);
}
@Override
public void running(ConfigurableApplicationContext context) {
System.out.println("starting...."+context);
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("starting...."+context);
}
}
4)创建HelloApplicationRunner 实现ApplicationRunner 接口
@Component
public class HelloApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(“ApplicationRunner… run…args = [” + args + “]”);
}
}
5)创建HelloCommandLineRunner实现 CommandLineRunner 接口
*/
@Component
public class HelloCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(" HelloCommandLineRunner...run"+ ArrayUtil.arrayToString(args));
}
}
6)新建META-INF和spring.factories 并编辑spring.factories
参考spring-boot jar包下的spring.factories文件
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
com.tang.bootapplication.linstener.HelloApplicationContextInitializer
# Application Listeners
org.springframework.context.SpringApplicationRunListener=\
com.tang.bootapplication.linstener.HelloSpringApplicationRunListener
7)启动测试