阅读导航
01、SpringBoot原理
Spring原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理
1.1、SpringBoot启动过程
-
创建 SpringApplication
-
保存一些信息。
//org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...) this.resourceLoader = resourceLoader;
-
判定当前应用的类型。ClassUtils。Servlet
this.webApplicationType = WebApplicationType.deduceFromClasspath();
-
bootstrappers
:初始启动引导器(List):去spring.factories文件中找 org.springframework.boot.Bootstrapper
public interface Bootstrapper { /** * Initialize the given {@link BootstrapRegistry} with any required registrations. * @param registry the registry to initialize */ void intitialize(BootstrapRegistry registry); }
-
找
ApplicationContextInitializer
;去spring.factories找ApplicationContextInitializersetInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
List<ApplicationContextInitializer<?>> initializers
-
找 ApplicationListener ;应用监听器。去spring.factories找ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
List<ApplicationListener<?>> listeners
-
-
运行 SpringApplication(run(…)方法)
-
StopWatch
StopWatch stopWatch = new StopWatch();
-
记录应用的启动时间
this.startTimeNanos = System.nanoTime();
-
创建引导上下文(Context环境)createBootstrapContext()
获取到所有之前的 bootstrappers 挨个执行 intitialize() 来完成对引导启动器上下文环境设置DefaultBootstrapContext bootstrapContext = createBootstrapContext(); || || private DefaultBootstrapContext createBootstrapContext() { DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext(); this.bootstrappers.forEach((initializer) -> initializer.intitialize(bootstrapContext)); return bootstrapContext; }
-
让当前应用进入headless模式。java.awt.headless
configureHeadlessProperty();
-
获取所有RunListener(运行监听器)【为了方便所有Listener进行事件感知】
listeners.starting(bootstrapContext, this.mainApplicationClass); || || private SpringApplicationRunListeners getRunListeners(String[] args) { Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class }; return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args), this.applicationStartup); }
-
getSpringFactoriesInstances 去spring.factories找
SpringApplicationRunListener
.
-
遍历 SpringApplicationRunListener 调用 starting 方法;
相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。void starting(ConfigurableBootstrapContext bootstrapContext, Class<?> mainApplicationClass) { doWithListeners("spring.boot.application.starting", (listener) -> listener.starting(bootstrapContext), (step) -> { if (mainApplicationClass != null) { step.tag("mainApplicationClass", mainApplicationClass.getName()); } }); }
-
-
保存命令行参数;ApplicationArguments
-
准备环境 prepareEnvironment();
-
返回或者创建基础环境信息对象。StandardServletEnvironment
-
配置环境信息对象。
-
读取所有的配置源的配置属性值。
-
绑定环境信息
-
监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成
-
-
创建IOC容器(createApplicationContext())
- 根据项目类型(Servlet)创建容器,
- 当前会创建 AnnotationConfigServletWebServerApplicationContext
-
准备ApplicationContext IOC容器的基本信息 prepareContext()
-
保存环境信息
-
IOC容器的后置处理流程。
-
应用初始化器;applyInitializers;
- 遍历所有的 ApplicationContextInitializer 。调用 initialize(context)。来对ioc容器进行初始化扩展功能
-
遍历所有的 listener 调用 contextPrepared。EventPublishRunListenr;通知所有的监听器contextPrepared
-
所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;
-
-
**刷新IOC容器。**refreshContext
- 创建容器中的所有组件(Spring注解)
-
容器刷新完成后工作?afterRefresh
-
所有监听 器 调用 listeners.started(context); 通知所有的监听器 started
-
**调用所有runners;**callRunners()
@FunctionalInterface public interface ApplicationRunner { /** * Callback used to run the bean. * @param args incoming application arguments * @throws Exception on error */ void run(ApplicationArguments args) throws Exception; } @FunctionalInterface public interface CommandLineRunner { /** * Callback used to run the bean. * @param args incoming main method arguments * @throws Exception on error */ void run(String... args) throws Exception; }
- 获取容器中的
ApplicationRunner
- 获取容器中的
CommandLineRunner
- 合并所有runner并且按照@Order进行排序
- 遍历所有的runner。调用 run 方法
- 获取容器中的
-
如果以上有异常,
- 调用Listener 的 failed
-
调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running
-
running如果有问题。继续通知 failed 。调用所有 Listener 的failed;通知所有的监听器failed
-
1.2、如果想在容器启动时做事,可以从下面组件入手
参考地址:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners
- ApplicationContextInitializer
- ApplicationListener
- SpringApplicationRunListener
- ApplicationRunner
- CommandLineRunner
其他
我的副业:觉得我的文章写得不错就支持一下我的副业吧
- 地址一:https://lsqstudy.gitee.io/post/d76a15aa112345.html
- 地址二:https://lsqstudy.github.io/post/d76a15aa112345.html
我的其他平台
作者做笔记不容易,请评个分吧!
- 如果觉得文章写行不错就点个 赞。✌
- 如果觉得写得好就 一键三连!✌✌✌
- 如果文章对你很有帮助,请 打赏。 💃💃💃💃💃