一.main方法中调用
二、run方法
三、先看看创建方法
1.设置资源加载器
resourceLoader:null
2.设置主要资源类
primarySources:
LinkedHashSet():[class com.example.springbootdemo.SpringbootdemoApplication
3.推断应用类型
webApplicationType:枚举WebApplicationType.SERVLET
this.webApplicationType = WebApplicationType.deduceFromClasspath();
4.引导程序初始化,找到引导初始化类的实例
加载META-INF/spring.factories目录下的内容,放到cache里cache.put(classLoader, result)//result共18条数据
a.第一个C:\Users\duqin\.m2\repository\org\springframework\boot\spring-boot\2.5.2\spring-boot-2.5.2.jar!\META-INF\spring.factories
b.第二个jar:file:/C:/Users/duqin/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.5.2/spring-boot-autoconfigure-2.5.2.jar!/META-INF/spring.factories
c.第三个jar:file:/C:/Users/duqin/.m2/repository/org/springframework/spring-beans/5.3.8/spring-beans-5.3.8.jar!/META-INF/spring.factories
bootstrapRegistryInitializers:ArrayList:size=05.
5.设置ApplicationContext的初始化器(factories文件中定义的接口类的实例)
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//共七个
6.设置org.springframework.context.ApplicationListener的实例
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//共8个
7.找到主类的class对象
mainApplicationClass:class com.example.springbootdemo.SpringbootdemoApplication
四、看看run方法
1.一些jwt的配置
configureHeadlessProperty() #jwt.headless
2.
获取并启动监听器getRunListeners(args)
(SpringApplicationRunListeners类
实例化时用到了factories文件中的EventPublishingRunListener)
SpringApplicationRunListeners listeners = getRunListeners(args);
获取的监听器为
EventPublishingRunListener
,从名字可以看出是启动事件发布监听器,主要用来发布启动事件。
3.构造容器环境prepareEnvironment
其中listeners.environmentPrepared第二次发布事件
获取到的监听器
4.创建上下文
context = createApplicationContext();//class org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
5.获取SpringBootExceptionReporter的实例
6.准备容器
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
a.执行容器后置处理器postProcessApplicationContext
if (this.addConversionService) {
context.getBeanFactory().setConversionService(
ApplicationConversionService.getSharedInstance());
}
b.应用初始化applyInitializers
c.准备监听器上下文
listeners.contextPrepared(context)
调用以下监听器的onApplicationEvent方法
d.load方法
load方法,会将SpringbootdemoApplication注入到beanDefinitionMap中
e.上下文加载
listeners.contextLoaded(context);
7.刷新容器,交由spring
refreshContext(context)
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
8、刷新容器后的扩展接口,默认为空
十一、
9、listeners.running
public void running(ConfigurableApplicationContext context) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.running(context);
}
}