当前位置: 首页 > 后端技术 > Java

8.1,SpringBoot启动流程——运行spring应用

时间:2023-04-01 18:23:51 Java

准备环境:privateConfigurableEnvironmentprepareEnvironment(SpringApplicationRunListenerslisteners,DefaultBootstrapContextbootstrapContext,ApplicationArgumentsapplicationArguments){//创建并配置环境//如果当前有上下文信息,获取并返回;如果没有则创建//例如保存底层环境变量ConfigurableEnvironmentenvironment=getOrCreateEnvironment();//配置环境(获取环境信息,命令行参数)configureEnvironment(environment,applicationArguments.getSourceArgs());//绑定环境信息ConfigurationPropertySources.attach(environment);//监听调用listener.environmentPrepared();通知所有的监听器当前环境已经准备好了listeners.environmentPrepared(bootstrapContext,environment);//设置环境信息到类的末尾(这里没有用到Tube)DefaultPropertiesPropertySource.moveToEnd(environment);//配置额外的Profiles,激活哪些环境(不关心)configureAdditionalProfiles(environment);//绑定工作(都是找东西,把一些属性绑定到一个值上)bindToSpringApplication(environment);//再次准备一些环境信息}//准备好后,再次将环境信息绑定到ConfigurationPropertySources。附加(环境);回归环境;}==================输入getOrCreateEnvironment()私有ConfigurableEnvironmentgetOrCreateEnvironment(){if(this.environment!=null){returnthis.environment;}switch(this.webApplicationType){caseSERVLET://如果是原生servlet编程returnnewStandardServletEnvironment();caseREACTIVE://如果是响应式编程returnnewStandardReactiveWebEnvironment();默认值:返回新的StandardEnvironment();}}=====================输入configureEnvironment()protectedvoidconfigureEnvironment(ConfigurableEnvironmentenvironment,String[]args){//向环境添加一些类型转换器if(this.addConversionService){ConversionServiceconversionService=ApplicationConversionService.getSharedInstance();environment.setConversionService((ConfigurableConversionService)conversionService);}//加载(读取)外部配置源//读取获取所有配置源的配置属性值configurePropertySources(environment,args);//激活配置文件环境configureProfiles(environment,args);}==========enterconfigurePropertySources()protectedvoidconfigurePropertySources(ConfigurableEnvironmentenvironment,String[]args){//获取所有配置源MutablePropertySourcessources=environment.getPropertySources();DefaultPropertiesPropertySource.ifNotEmpty(this.defaultProperties,sources::addLast);//命令行信息if(this.addCommandLineProperties&&args.length>0){Stringname=CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;如果(来源。包含(名称)){PropertySourcesource=sources.get(名称);CompositePropertySourcecomposite=newCompositePropertySource(名称);composite.addPropertySource(newSimpleCommandLinePropertySource("springApplicationCommandLineArgs",args));composite.addPropertySource(来源);sources.replace(名称,复合);}else{sources.addFirst(newSimpleCommandLinePropertySource(args));}}}==============进入environmentPrepared()voidenvironmentPrepared(ConfigurableBootstrapContextbootstrapContext,ConfigurableEnvironmentenvironment){doWithListeners("spring.boot.application.environment-prepared",(listener)->listener.environmentPrepared(bootstrapContext,environment));}所有配置源