Re exploration of automatic configuration principle

 

Automatic configuration principle

 

What can a configuration file write? How do you write it?

There are a lot of configurations in the official documentation of SpringBoot, which we can't remember all

 

 

 

Analyze the principle of automatic configuration

We take Http encoding autoconfiguration (Http encoding autoconfiguration) as an example to explain the principle of autoconfiguration;

//Indicates that this is a configuration class. Like the previously written configuration file, you can also add components to the container;
@Configuration 

//Starts the of the specified class ConfigurationProperties Function;
  //Enter this HttpProperties View the corresponding values in the configuration file and HttpProperties Bind;
  //And put HttpProperties Add to ioc In container
@EnableConfigurationProperties({HttpProperties.class}) 

//Spring bottom@Conditional annotation
  //According to different conditions, if the specified conditions are met, the configuration in the whole configuration class will take effect;
  //This means to judge whether the current application is web Application. If yes, the current configuration class takes effect
@ConditionalOnWebApplication(
    type = Type.SERVLET
)

//Determine whether the current project has this class CharacterEncodingFilter;SpringMVC The garbled code is being solved by the filter;
@ConditionalOnClass({CharacterEncodingFilter.class})

//Determine whether a configuration exists in the configuration file: spring.http.encoding.enabled;
  //If it does not exist, the judgment is also valid
  //Even if it is not configured in our configuration file pring.http.encoding.enabled=true,It also takes effect by default;
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)

public class HttpEncodingAutoConfiguration {
    //He's already with SpringBoot Your profile is mapped
    private final Encoding properties;
    //When there is only one constructor with parameters, the value of the parameter will be taken from the container
    public HttpEncodingAutoConfiguration(HttpProperties properties) {
        this.properties = properties.getEncoding();
    }
    
    //Add a component to the container. Some values of this component need to be properties Get in
    @Bean
    @ConditionalOnMissingBean //Determine whether the container does not have this component?
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
        return filter;
    }
    //. . . . . . . 
}

 

One sentence summary: determine whether this configuration class is effective according to different current conditions!
  • Once this configuration class takes effect; This configuration class will add various components to the container;

  • The properties of these components are obtained from the corresponding properties classes, and each property in these classes is bound to the configuration file;

  • All properties that can be configured in the configuration file are encapsulated in the xxxproperties class;

  • The attribute class corresponding to a function can be referenced for what can be configured in the configuration file

//Gets the specified value and value from the configuration file bean Bind to properties of
@ConfigurationProperties(prefix = "spring.http") 
public class HttpProperties {
    // .....
}

 

Let's try the prefix in the configuration file and see the tips!

 

 

This is the principle of automatic assembly!

 

quintessence

1. SpringBoot boot will load a large number of auto configuration classes

2. Let's see if the functions we need are in the auto configuration class written by SpringBoot by default;

3. Let's see which components are configured in this automatic configuration class; (as long as the component we want to use exists in it, we don't need to configure it manually)

4. When adding components to the automatic configuration class in the container, some properties will be obtained from the properties class. We only need to specify the values of these attributes in the configuration file;

Xxxxxxautoconfiguration: automatic configuration class; Add components to container

Xxxproperties: encapsulates the related properties in the configuration file;

 

Understanding: @ Conditional

After understanding the principle of automatic assembly, let's pay attention to a detail. The automatic configuration class must take effect under certain conditions;

@Conditional derived annotation (the native @ conditional function of Spring annotation version)

Function: only when the conditions specified by @ Conditional are satisfied can components be added to the container and all contents in the configuration configuration take effect;

 

 

So many automatic configuration classes can only take effect under certain conditions; In other words, we have loaded so many configuration classes, but not all of them have taken effect.

How do we know which auto configuration classes work?

We can enable the debug=true attribute; To let the console print the automatic configuration report, so that we can easily know which automatic configuration classes are effective;

#Open the debugging class of springboot
debug=true

 

Positive matches: (auto configuration class enabled: positive matches)

Negative matches: (automatic configuration class without startup and successful matching: negative matching)

Unconditional classes: (unconditional classes)

[Demo: view the output log]

Master the principle of absorption and understanding, that is, you can remain unchanged and respond to changes!

Posted by tylerdurden on Sun, 17 Apr 2022 03:29:26 +0930