SpringBoot启动流程原理解析(二) (2)

由于我们是添加了web的依赖 getOrCreateEnvironment()返回的是一个standardservletEnviroment 标准的servlet环境

2.1 配置环境 protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { if (this.addConversionService) { // 嵌入式的转换器 ConversionService conversionService = ApplicationConversionService.getSharedInstance(); environment.setConversionService((ConfigurableConversionService) conversionService); } // 配置属性资源文件 configurePropertySources(environment, args); // 配置文件 configureProfiles(environment, args); }

应用嵌入的转换器ApplicationConversionService

public static void configure(FormatterRegistry registry) { DefaultConversionService.addDefaultConverters(registry); DefaultFormattingConversionService.addDefaultFormatters(registry); // 格式转换 addApplicationFormatters(registry); // 类型转换 addApplicationConverters(registry); } ===============格式转换================= public static void addApplicationFormatters(FormatterRegistry registry) { registry.addFormatter(new CharArrayFormatter()); registry.addFormatter(new InetAddressFormatter()); registry.addFormatter(new IsoOffsetFormatter()); } ========================类型转换=================== public static void addApplicationConverters(ConverterRegistry registry) { addDelimitedStringConverters(registry); registry.addConverter(new StringToDurationConverter()); registry.addConverter(new DurationToStringConverter()); registry.addConverter(new NumberToDurationConverter()); registry.addConverter(new DurationToNumberConverter()); registry.addConverter(new StringToPeriodConverter()); registry.addConverter(new PeriodToStringConverter()); registry.addConverter(new NumberToPeriodConverter()); registry.addConverter(new StringToDataSizeConverter()); registry.addConverter(new NumberToDataSizeConverter()); registry.addConverter(new StringToFileConverter()); registry.addConverter(new InputStreamSourceToByteArrayConverter()); registry.addConverterFactory(new LenientStringToEnumConverterFactory()); registry.addConverterFactory(new LenientBooleanToEnumConverterFactory()); if (registry instanceof ConversionService) { addApplicationConverters(registry, (ConversionService) registry); } } 2.2 环境准备完成

同上面启动监听事件,这次的环境准备也是同样的代码

@Override public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) { this.initialMulticaster.multicastEvent( // 创建一个应用环境准备事件对象 new ApplicationEnvironmentPreparedEvent(bootstrapContext, this.application, this.args, environment)); }

debug进去之后代码跟AppLicationstrigevent 事件对象是一样的。不再赘述。
不过这里是7个监听器对象

3.配置忽略的bean

configureIgnoreBeanInfo(environment);

4.打印banner

这是SpringBoot默认的启动时的图标
Banner printedBanner = printBanner(environment);

SpringBoot启动流程原理解析(二)


这个是可以自定义的,也可以是图篇或是文本文件中的图形

5.创建容器

紧接着上一篇,接下来就是创建容器

protected ConfigurableApplicationContext createApplicationContext() { return this.applicationContextFactory.create(this.webApplicationType); } 6.准备应用上下文 private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { // 设置环境参数 context.setEnvironment(environment); // 设置后处理应用上下文 postProcessApplicationContext(context); //把从spring.factories中加载的org.springframework.bt.context.ConfigurationwarningsApplicationContextIitiaLizer,进行初始化操作 applyInitializers(context); //EventPubLishingRunListener发布应用上下文事件 listeners.contextPrepared(context); // 打印启动日志 bootstrapContext.close(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { //注册一个字是springAppLicationArguments单例的bean beanFactory.registerSingleton("springBootBanner", printedBanner); } if (beanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.lazyInitialization) { context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); } // Load the sources 获取所有资源 Set<Object> sources = getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); // 创建BeanDefinitionLoader加载器加载注册所有的资源 load(context, sources.toArray(new Object[0])); // 同之前,发布应用上下文 加载事件 listeners.contextLoaded(context); } 7.刷新应用上下文

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzyjd.html