、、springboot启动流程—主类上
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
然后其调用了下面这个
primarySources是传入的主类
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
new SpringApplication(primarySources)
主要是先填入部分的属性
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
//资源加载器传入的是null
this.resourceLoader = resourceLoader;
//非空判定
Assert.notNull(primarySources, "PrimarySources must not be null");
//主类封装为LinkedHashSet
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//从类路径中推测web应用类型,这个主要是看存在那个类,一般是Servlet
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//使用SpringFactoriesLoader.loadFactoryNames加载
//ApplicationContextInitializer.class ApplicationListener.class并设置
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//是通过获取当前的方法栈new RuntimeException().getStackTrace();
//然后找到使用main方法的那一个类,然后返回该类
this.mainApplicationClass = deduceMainApplicationClass();
}
run(args);
public ConfigurableApplicationContext run(String... args) {
//记录事件的对象
StopWatch stopWatch = new StopWatch();
stopWatch.start();
//ApplicationContext的子接口,上下文
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
//获取监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
//启动监听器
//就是this.initialMulticaster.multicastEvent(new ApplicationStartingEvent
listeners.starting();
try {
//封装保存args
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//构造应用上下文环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//处理需要忽略的bean
configureIgnoreBeanInfo(environment);
//打印banner
Banner printedBanner = printBanner(environment);
//初始化应用上下文,IOC容器,也就是DefaultListableBeanFactory会在这时候创建并添加入applicationcontext
context = createApplicationContext();
//SpringBootExceptionReporter的实例获取(用于支持springboot启动错误)
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//刷新上下文之前的准备工作
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//刷新上下文
refreshContext(context);
//刷新上下文后置,默认空实现,在刷新上下文之后的扩展接口
afterRefresh(context, applicationArguments);
//事件记录停止
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
//发布容器启动完成事件
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}