我在看springboot的原始碼,不太懂prepareEnvironment函式中為什么要有這個if代碼塊。
if的條件中是判斷是否為自定義環境,如果是自定義環境那么就不會執行if代碼塊,如果是通過getOrCreateEnvironment創建的環境,說明不是自定義環境,那會進入if代碼塊執行。
進入if代碼塊后,會對環境進行轉換,看convertEnvironmentfINecessary函式,最開始的型別判斷應該一定會回傳true的呀,在其他地方也沒發現誰呼叫過convertEnvironmentIfNecessary這個函式,說明convertEnvironmentIfNecessary函式中的第二個return不會被執行。
那這么寫的作用是什么,感謝各位大佬指導下。
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
listeners.environmentPrepared(bootstrapContext, environment);
DefaultPropertiesPropertySource.moveToEnd(environment);
configureAdditionalProfiles(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
StandardEnvironment convertEnvironmentIfNecessary(ConfigurableEnvironment environment,
Class<? extends StandardEnvironment> type) {
if (type.equals(environment.getClass())) {
return (StandardEnvironment) environment;
}
return convertEnvironment(environment, type);
}
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
}
switch (this.webApplicationType) {
case SERVLET:
return new StandardServletEnvironment();
case REACTIVE:
return new StandardReactiveWebEnvironment();
default:
return new StandardEnvironment();
}
}
private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
switch (this.webApplicationType) {
case SERVLET:
return StandardServletEnvironment.class;
case REACTIVE:
return StandardReactiveWebEnvironment.class;
default:
return StandardEnvironment.class;
}
}
uj5u.com熱心網友回復:
做環境預處理是因為springboot支持不同的啟動方式,它必須要清楚當前是什么環境才能決定以哪種方式啟動,不想spring mvc,你的web server是外部的轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284023.html
標籤:Java EE
下一篇:求救!!!!大牛們