Home > Java > javaTutorial > body text

Analysis of the principles of Springboot-yaml configuration and automatic configuration

王林
Release: 2023-05-13 15:25:06
forward
1077 people have browsed it

Version Arbitration Center

spring dependencies help us rely on many commonly used jar packages. Importing these jar packages does not require a version number
For example:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
Copy after login

Automatic configuration principle

Configuration file configuration debug: true can print the automatic configuration report on the console. You can print all started automatic configuration and unstarted automatic configuration classes.

@SpringBootApplication
Marked in a certain class, indicating 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 {
Copy after login

@EnableAutoConfiguration: Enable automatic configuration, so we don’t have to do a lot of configuration manually

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
Copy after login

@ AutoConfigurationPackage
Enter the main configuration class All components under the package will be scanned into the spring container.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
Copy after login

AutoConfigurationImportSelector
Use @import: to import a component into the container, which will load all automatic configuration classes, such as mysql, web, etc. Wait
Finally, we will go to META-INF/spring.factories to find all the automatic configuration classes and load them into the container. These automatic configuration classes have done a lot of the configuration we used to do with spring.

yaml syntax

Literal

Strings do not need to be quoted by default, single quotes and double quotes have special purposes

Single quotes are special Escape, such as \n output or \n
double quotation mark special characters will not be escaped, such as \n output is a space
will be escaped the same as adding single quotes

Loose binding

The way to write attributes in camel case is the same as adding a dash - or underline_. When converted to entity classes, they are all camel case. However, this can only be used in configurationProperties and cannot be used. Using the

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
Copy after login

annotation in the @Value annotation can allow the custom configuration in the yaml configuration to have prompts

Use the

@PropertySource annotation together with the @PropertySource annotation You can load other specified files

@PropertySource(value = "classpath:user.properties")
Copy after login

Use together with @ImportResource

Import the spring configuration file and make it take effect

@ImportResource(locations={"classpath:mybatis.xml"})
Copy after login

Configuration file placeholder

${random.int} Use the random number provided by yaml
${server.port} Use the previously configured value
${server.name: Hello} If there is no value, use the default value

profile

Activate to specify different configuration environments

Command line activation can add –spring.profiles.active=dev
Virtual machine parameters Activate -Dspring.profiles.active=dev

Loading sequence of configuration files

file: ./config/ config directory under the project root path
file: ./ project Root directory
classpath: config/
classpath: /
All files will be loaded, with priorities from top to bottom, from high to low, with higher ones overwriting lower ones. Different configurations will take effect. , complementary.
You can also change the configuration file location through –spring.config.location when deploying the project. The configuration file loaded in the project is complementary to the configuration file specified here.

The above is the detailed content of Analysis of the principles of Springboot-yaml configuration and automatic configuration. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template