Springboot启动源码流程图
2023-02-25 17:21:45 0 举报
Springboot启动源码流程图
作者其他创作
大纲/内容
@SpringBootApplication注解是@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个注解注册的复合注解。@SpringBootConfiguration:内部有@Configuration注解,表示配置类@ComponentScan:包扫描路径,默认为当前注解标注的类所在包路径。还可以指定包含或者排除或包含自动配置类@EnableAutoConfiguration:是Springboot实现自动配置的核心注解,在该注解内部导入了@Import(AutoConfigurationImportSelector.class),用于解析和注册自动配置类的
根据条件注解排除不符合的自动配置类configurations = getConfigurationClassFilter().filter(configurations);
将主类BeanDefinition注册到Springboot容器中this.annotatedReader.register(source);
拿到META-INF/spring.factoriesspan style=\"font-size: inherit;\
Spring在解析配置类时会调用AutoConfigurationImportSelector的selectImports方法,但是由于该类实现了Group接口,所以最终会调用到process方法
推到出当前主类,带有main方法的那个类就是主类this.mainApplicationClass = deduceMainApplicationClass();
封装参数,备用ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector) .getAutoConfigurationEntry(annotationMetadata);
applicationContext实现类是ServletWebServerApplicationContext容器applicationContext.refresh();
循环调用(listener) -> listener.starting(bootstrapContext)方法,这里也就是EventPublishingRunListener的starting方法this.listeners.forEach(listenerAction);
拿到META-INF/spring.factories中key为SpringApplicationRunListener的监听器(这里可以拿到一个:EventPublishingRunListener)SpringApplicationRunListeners listeners = getRunListeners(args);
for (Object source : this.sources) { load(source); }
创建Springboot启动过程中的容器,属于SpringbootDefaultBootstrapContext bootstrapContext = createBootstrapContext();
从META-INF/spring.factories获取Springboot启动时的监听器setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
创建Severlet容器,这里是内嵌TomcatonRefresh();
调用父类进行容器刷新:web容器启动,这里是Tomact容器,配置类解析、自动配置类解析、创建单例Bean等操作。这里只看SpringBoot相关操作,Spring中的步骤详见连接super.refresh();
this.autoConfigurationEntries.add(autoConfigurationEntry);
Springboot启动前做一下准备new SpringApplication(primarySources)
获取容器,如果不为null表示有外置web容器,如果为null就去创建web容器ServletContext servletContext = getServletContext();
循环调用bootstrapRegistryInitializers中对象的initialize方法,并将bootstrapContext作为参数传给bootstrapRegistryInitializer,用户就可以在initialize方法中自定义的对bootstrapContext进行修改。注:bootstrapRegistryInitializer是从META-INF/spring.factories读取的,Springboot默认是没有的
AutoConfigurationImportSelector.process()
实例 化Springboot容器对象:ServletWebServerApplicationContextcontext = createApplicationContext();
从main()方法开始
this.webServer = factory.getWebServer(getSelfInitializer());
从META-INF/spring.factories中获取Springboot 的bootstrapContext启动初始化器。bootstrapRegistryInitializer
new SpringApplication(primarySources).run(args);
(listener) - listener.starting(bootstrapContext)
创建TomactTomcat tomcat = new Tomcat();
刷新容器refreshContext(context);
设置类型转换服务context.getBeanFactory().setConversionService(context.getEnvironment().getConversionService());
doWithListeners(\"spring.boot.application.starting\
发布springboot启动事件this.initialMulticaster.multicastEvent
发布容器准备完成事件listeners.contextLoaded(context);
run()
refresh(context);
启动SpringBootrun(args)
Springboot自动配置类解析
排除指定的自动配置类configurations.removeAll(exclusions);
load((Class<?>) source);
从META-INF/spring.factories获取创建Springboot容器初始化器,用来初始化contentapplyInitializers(context)
设置环境配置信息context.setEnvironment(environment);
放入主配置类this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
配置类解析、包扫描。Springboot的自动配置类也是在这一步注册为BeanDefinition(获取META-INF/spring.factories中的自动配置类)invokeBeanFactoryPostProcessors(beanFactory);
配置的读取优先级,按照下面的顺序读取配置
推到当前环境类型,当前为severlet环境this.webApplicationType = WebApplicationType.deduceFromClasspath();
同步或者异步发布事件multicastEvent()
loader.load();
发布事件:容器初始化完成事件listeners.contextPrepared(context);
配置类去重configurations = removeDuplicates(configurations);
从META-INF/spring.factories获取创建Springboot容器初始化器,用来初始化contentsetInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
start
关闭springboot启动引导容器bootstrapContext.close(context);
postProcessApplicationContext(context)
createWebServer();
0 条评论
下一页