Home > Back-end >  Springboot environment transformation problem in prepareEnvironment function
Springboot environment transformation problem in prepareEnvironment function

Time:05-15

I'm watching springboot source, don't know much about why do you want to have this in prepareEnvironment function if a code block,

If the condition is whether the judgment for custom in the environment, if it is a custom environment so will not perform the if block, if by getOrCreateEnvironment create environment, that is not a custom environment, it will enter the if block of code execution,

After entering the if block, will be transformed to the environment, see convertEnvironmentfINecessary function, the first type judgment should be bound to return true ah, has not found in other places who call convertEnvironmentIfNecessary the function, the second return instructions convertEnvironmentIfNecessary function would not be executed,

That what is the function of, thank you for your bosses, under the guidance of the

 
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 (! Enclosing isCustomEnvironment) {
The environment=new EnvironmentConverter (getClassLoader ()). ConvertEnvironmentIfNecessary (environment,
DeduceEnvironmentClass ());
}
ConfigurationPropertySources. Attach (environment);
Return the environment;
}

StandardEnvironment convertEnvironmentIfNecessary (ConfigurableEnvironment environment,
Class<? Extends StandardEnvironment> Type) {
If (type. The equals (environment. GetClass ())) {
Return (StandardEnvironment) environment;
}
Return convertEnvironment (environment, type);
}

Private ConfigurableEnvironment getOrCreateEnvironment () {
If (this. The environment!=null) {
Return this. The environment;
}
The switch (enclosing webApplicationType) {
In case the SERVLET:
Return new StandardServletEnvironment ();
Case REACTIVE:
Return new StandardReactiveWebEnvironment ();
Default:
Return new StandardEnvironment ();
}
}

Private Class<? Extends StandardEnvironment> DeduceEnvironmentClass () {
The switch (enclosing webApplicationType) {
In case the SERVLET:
Return StandardServletEnvironment. Class;
Case REACTIVE:
Return StandardReactiveWebEnvironment. Class;
Default:
Return StandardEnvironment. Class;
}
}

CodePudding user response:

Do environment preprocessing because springboot support different startup mode, it has to be clear what is the current environment can decide which way to start, don't want to spring MVC, your web server is outside
  • Related