The underlying principle of SpringBoot automatic configuration (interview)

Automatic configuration principle

A man of great worship

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

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

Analyze the principle of automatic configuration

We take * * Http encoding autoconfiguration) * * as an example to explain the principle of automatic configuration;

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

//Start the ConfigurationProperties function of the specified class;
  //Enter the HttpProperties view and bind the corresponding values in the configuration file with HttpProperties;
  //And add HttpProperties to the ioc container
@EnableConfigurationProperties({HttpProperties.class}) 

//Spring underlying @ 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 a web application. If so, the current configuration class will take effect
@ConditionalOnWebApplication(
    type = Type.SERVLET
)

//Judge whether the current project has this class CharacterEncodingFilter; Filter for garbled code resolution in spring MVC;
@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 pring. Is not configured in our configuration file http. encoding. Enabled = true, which is also effective by default;
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)

public class HttpEncodingAutoConfiguration {
    //He has mapped to the SpringBoot configuration file
    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 obtained from properties
    @Bean
    @ConditionalOnMissingBean //Determine that 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 the 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 xxproperties class;
  • The attribute class corresponding to a function can be referenced for what can be configured in the configuration file
//Get the specified value from the configuration file and bind it with the properties of the bean
@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 look at 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;

**Xxxautoconfiguration: automatic configuration class** Add components to container

Xxxproperties: encapsulates 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 met can components be added to the container and all contents in the configuration configuration take effect;

So many auto configuration classes can only take effect under certain conditions; In other words, we loaded so many configuration classes, but not all of them took 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: (no startup, no matching successful automatic configuration class: 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!

Keywords: Spring Boot

Added by god_zun on Sun, 02 Jan 2022 10:39:34 +0200