Principle Analysis of Springboot yaml Configuration and Automatic Configuration
- 2021-09-24 22:19:35
- OfStack
Version arbitration center
spring dependencies help us rely on a lot of commonly used jar packages, import these jar packages do not need a version number
Such as:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
Automatic configuration principle
Configuration File Configuration debug: true can print auto-configuration reports on the console. All auto-configuration classes that are started and those that are not started can be printed.
@SpringBootApplication
Label on a class to indicate that this class is the main startup class of springboot.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@ EnableAutoConfiguration: Turn on automatic configuration, so we don't have to do a lot of configuration manually
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@ AutoConfigurationPackage
All components under the package where the main configuration class is located will be scanned into the spring container.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
AutoConfigurationImportSelector
Import 1 component into the container via @ import: This component loads all auto-configuration classes, such as mysql, web, and so on
Eventually, we'll find all the auto-configuration classes at META-INF/spring. factories and load them into the container. These auto-configuration classes do away with a lot of configurations we used to do with spring.
yaml Syntax
Literal quantity
Strings do not need quotation marks by default, and single quotation marks and double quotation marks have special intentions
Single quotes are specifically escaped, such as\ n output or\ n
Double quotation mark special characters are not escaped, such as\ n output is 1 space
If you don't add and add single quotation marks, you will escape
Loose binding
Attributes are written as hump and underlined-or underlined _ 1, and are converted to entity classes as hump. However, this can only be used in configurationProperties, not in @ Value annotations
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
This annotation can make the user-defined configuration in yaml configuration prompt
And @ PropertySource annotation 1
@ PropertySource annotations can load additional files specified
@PropertySource(value = "classpath:user.properties")
And @ ImportResource1
Import the configuration file of spring for it to take effect
@ImportResource(locations={"classpath:mybatis.xml"})
Profile placeholder
${random. int} uses random numbers supplied by yaml
${server. port} Use the previously configured values
${server. name: Hello} Use the default value if there is no value
profile
Activation specifies different configuration environments
Command line activation can add spring. profiles. active=dev
Virtual machine parameter activation-Dspring. profiles. active=dev
Load order of configuration files
file:./config/config Directory under Project Root Path
file:./Project Root
classpath: config/
classpath: /
All files will be loaded into, from top to bottom priority from high to low, the high will overwrite the low content. Different configurations will take effect, complementing each other.
You can also change the configuration file location by using spring. config. location when deploying the project. The configuration file loaded in the project complements the configuration file specified here.
The above is the Springboot-yaml configuration and auto-configuration principle of the details, more information about Springboot auto-configuration please pay attention to other related articles on this site!