Three, SpringBoot-- automatic configuration principle

How to write a profile

See configuration documentation
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#common-application-properties

2 automatic configuration principle

You can view @HttpEncodingAutoConfiguration
1), SpringBoot start when the load master class configuration, open the automatic configuration @EnableAutoConfiguration

2), @ EnableAutoConfiguration effect:
using EnableAutoConfigurationImportSelector introduction to some of the components in the container
can be viewed selectImports (process contents);
List Configurations = getCandidateConfigurations (annotationMetadata, Attributes); obtaining candidates configuration

SpringFactoriesLoader.loadFactoryNames () scans all automatic configuration META-INF / spring.factories to scan the contents of these files packaged into the jar package properties object class path. Obtained from a value corresponding to the properties EnableAutoConfiguration.class class (class name), and then add them to the container, the values of all the META-INF EnableAutoConfiguration classpath / spring.factories added to the configuration inside the vessel;
spring.factories file reads as follows
Here Insert Picture Description

Each of these classes are xxxAutoConfiguration a container assembly, are added to the vessel; to do with their auto-configuration;
3), each class is automatically configure auto-configuration;

4) to HttpEncodingAutoConfiguration (Http autoconfiguration coding) as an example to explain the principle of auto-configuration;

@Configuration // indicates that this is a configuration class, as with previous written profile, you can also add components to the container

@EnableConfigurationProperties (HttpEncodingProperties.class) // start the specified class
ConfigurationProperties function; the profile value corresponding to bind HttpEncodingProperties; ioc and added to the vessel HttpEncodingProperties

@ConditionalOnWebApplication // Spring underlying @Conditional annotation (the Spring annotation Edition), depending on the conditions,If the specified condition is satisfied, the entire configuration is disposed inside the class will take effect; determining whether the current application is a web application, if it is, the current configuration classes take effect

@ConditionalOnClass (CharacterEncodingFilter.class) // bottom @Conditional annotation item currently have this determination based CharacterEncodingFilter; solution for distortion in a filter SpringMVC;

@ConditionalOnProperty (prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) // bottom @Conditional determines whether there is a comment spring.http.encoding.enabled profile configuration; if not, judgment is established
// even if we do not configure pring.http.encoding.enabled = true configuration file , the default is in effect;

public class HttpEncodingAutoConfiguration {

// He has SpringBoot profiles and mapping the
private final HttpEncodingProperties properties;

// next only a constructor parameter, the value of the parameter will take a container from
public HttpEncodingAutoConfiguration (HttpEncodingProperties Properties) {
this.properties = Properties;
}

@Bean // add a component to the container, this component need to get some value from properties in
@ConditionalOnMissingBean (CharacterEncodingFilter.class) // judge container does not have this component?
public CharacterEncodingFilter characterEncodingFilter () {

CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}

Depending on the conditions of the current judgment, decide whether the class configuration take effect? Once this configuration classes take effect; this configuration class will be added to the various components of the container; the properties of these components are obtained from the corresponding properties of the class, each of these classes which is a property of binding and profile;

5), all the attributes in the configuration file (application.yml application.properties or file) that can be configured are encapsulated in a class by xxxxProperties'; what configuration file can be configured to reference a function
capable of corresponding to the attribute class

@ConfigurationProperties (prefix = "spring.http.encoding") // Get the value of the specified attribute from the bean binding profile

public class HttpEncodingProperties {
public static final Charset DEFAULT_CHARSET = Charset.forName(“UTF‐8”);

Essence:
1), SpringBoot will start automatically configured to load a lot of class
2), we look at the features we need are not automatically configure the default class SpringBoot written;
3), we will look at this automatic configuration class in which components are configured in the end ; (as long as we have to use component, we do not need to come equipped)
4), to automatically configure the container class when adding components, will get certain properties from the properties in class. We can specify values for these properties in the configuration file;

xxxxAutoConfigurartion: automatic configuration class; add a component to a container
xxxxProperties: encapsulation configuration file related attributes

Here Insert Picture Description

Automatic mode configuration generic class
- xxxAutoConfiguration: Class Automatic Configuration
- xxxProperties: Class Attribute Configuration
- value yml / properties file that can be configured to derived [class attribute configuration]

Automatic Configuration class must take effect under certain conditions;
how do we know which automatically configures the class into effect;
we can enable debug = true property; to have the console automatically print a configuration report, so that we can easily know which auto-configuration class into force;

3 configuration file debug = true to view a detailed configuration report automatically

positive for the automatic configuration of a
negtive is not configured on
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/m0_38143867/article/details/93205246
Recommended