starting
方法
这个starting
方法实际上就是通过事件广播发布了一个应用启动事件
private final SimpleApplicationEventMulticaster initialMulticaster;public void starting() {this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));}
复制
准备启动环境然后我们再回过去看SpringApplication#prepareEnvironment
方法 , 这个方法是准备启动环境的意思
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {// 获取或者创建一个配置环境ConfigurableEnvironment environment = this.getOrCreateEnvironment();// 配置环境this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());// 通过启动类应用监听器发布环境准备事件listeners.environmentPrepared((ConfigurableEnvironment)environment);// 将配置环境绑定到SpringApplicationthis.bindToSpringApplication((ConfigurableEnvironment)environment);if (!this.isCustomEnvironment) {// 如果是非自定义环境则根据需要转换成推断出的Web环境environment = (new EnvironmentConverter(this.getClassLoader())).convertEnvironmentIfNecessary((ConfigurableEnvironment)environment, this.deduceEnvironmentClass());}// 通过环境变量添加属性配置源ConfigurationPropertySources.attach((Environment)environment);// 返回经过处理的配置环境return (ConfigurableEnvironment)environment;}
复制
getOrCreateEnvironment方法private ConfigurableEnvironment environment;private ConfigurableEnvironment getOrCreateEnvironment() {if (this.environment != null) {// this.environment不为空则直接返回return this.environment;} else {switch(this.webApplicationType) {// 根据web应用类型创建环境case SERVLET:// servlet web应用返回标准StandardServletEnvironment实例return new StandardServletEnvironment();case REACTIVE:// reactive web应用环境返回StandardReactiveWebEnvironment实例return new StandardReactiveWebEnvironment();default:// 默认返回非web应用的StandardEnvironment实例return new StandardEnvironment();}}}
复制
前面的WebApplicationType#deduceFromClasspath
方法码分析中我们知道返回的是一个SERVLET枚举 。因此 , spring-boot
项目中具有spring-boot-starter-web
起步依赖时getOrCreateEnvironment
方法返回的是一个StandardServletEnvironment
实例
SpringApplicationRunListeners#environmentPrepared接下来我们进入SpringApplicationRunListeners#environmentPrepared
方法
public void environmentPrepared(ConfigurableEnvironment environment) {Iterator var2 = this.listeners.iterator();while(var2.hasNext()) {SpringApplicationRunListener listener = (SpringApplicationRunListener)var2.next();// 通过迭代遍历启动监听器发布环境准备事件listener.environmentPrepared(environment);}}
复制
SpringApplication#bindToSpringApplication方法准备好环境后进入SpringApplication#bindToSpringApplication
方法
protected void bindToSpringApplication(ConfigurableEnvironment environment) {try {Binder.get(environment).bind("spring.main", Bindable.ofInstance(this));} catch (Exception var3) {throw new IllegalStateException("Cannot bind to SpringApplication", var3);}}
复制
Binder#get方法public static Binder get(Environment environment) {// 这里传入了一个属性源占位符解析器类实例参数return new Binder(ConfigurationPropertySources.get(environment), new PropertySourcesPlaceholdersResolver(environment));}
复制
Binder类构造方法【SpringBoot启动流程源码分析】public Binder(Iterable<ConfigurationPropertySource> sources, PlaceholdersResolver placeholdersResolver) {this(sources, placeholdersResolver, (ConversionService)null, (Consumer)null);}public Binder(Iterable<ConfigurationPropertySource> sources, PlaceholdersResolver placeholdersResolver, ConversionService conversionService, Consumer<PropertyEditorRegistry> propertyEditorInitializer) {Assert.notNull(sources, "Sources must not be null");this.sources = sources;this.placeholdersResolver = placeholdersResolver != null ? placeholdersResolver : PlaceholdersResolver.NONE;this.conversionService = conversionService != null ? conversionService : ApplicationConversionService.getSharedInstance();this.propertyEditorInitializer = propertyEditorInitializer;}
经验总结扩展阅读
- 一次SpringBoot版本升级,引发的血案
- 个人办理社保的流程是怎样的 个人怎么交社保
- 回门酒参加的都是谁 回门酒的流程
- 88vip双十一大额消费券领取流程2022 淘宝88vip双十一大额优惠券使用规则
- SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器
- 房屋确权必须是户主吗 房屋确权的办理流程
- 公寓能不能抵押 房产证抵押贷款流程是什么
- SpringBoot 02: 初识SpringBoot
- 二手房网签注意事项 二手房网签流程
- 买房网签之后还有什么流程