ConfigurationProperties of Spring Boot automatic assembly

ConfigurationProperties of Spring Boot automatic assembly

Spring Boot features

Spring Boot介绍:
Create stand-alone Spring applications 
-- 创建一个独立的Spring应用
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files) 
-- 嵌入式的web服务器Tomcat、Jetty、Undertow(无需部署 war 文件)
Provide opinionated 'starter' dependencies to simplify your build configuration
-- 提供建议的 "启动" 依赖关系, 以简化生成配置
Automatically configure Spring and 3rd party libraries whenever possible
-- 尽可能自动配置 spring 和第三方库
Provide production-ready features such as metrics, health checks and externalized configuration
-- 具备为生产准备的特性,如指标、运行状况检查和外部化配置
Absolutely no code generation and no requirement for XML configuration
-- 尽可能的无需代码生成并且无XML配置

Automated assembly process

  1. The dependencies between classes are realized through various annotations. When the container starts, Application.run will call the selectImports method of EnableAutoConfigurationImportSelector.class (actually the method of its parent class)

  2. The selectImports method will eventually call the SpringFactoriesLoader.loadFactoryNames method to get a comprehensive list of commonly used BeanConfiguration

  3. The loadFactoryNames method will read FACTORIES_RESOURCE_LOCATION (that is, spring.factories under spring-boot-autoconfigure.jar), and get the fully qualified name ClassName of all Spring-related Beans, about 120

  4. The selectImports method continues to call filter(configurations, autoConfigurationMetadata); this time, it will filter one by one according to the conditions in these BeanConfigurations, the most important thing is
    @ConditionalOnClass, this condition annotation will be searched under the classpath, whether there is this condition dependency in the jar package Class, so you must have the corresponding jar package to have these dependent classes, and then generate some default configuration beans required by the IOC environment

  5. Finally, inject the qualified BeanConfiguration into the attribute value in the default EnableConfigurationPropertie class and inject it into the IOC environment

ConfigurationProperties attribute automatic assembly

file type

The configuration file supports two kinds of yml and properties, application.properties file and application.yml, both of which can be automatically recognized and loaded by SpringBoot. The two file formats are generally placed under the src/main/resource directory.

In addition to this is the custom configuration file, the general format is .properties.

Property automatic assembly one

Set properties configuration application.properties

test.config.userName=sxs
test.config.password=123456
[email protected]
test.config.phoneNumber=176xxxx1573

Attribute mapping class

@Configuration
@ConfigurationProperties(prefix = "test.config")
// @PropertySource("classpath:/test-config.properties")
public class TestConfiguration {
    
    

    private String userName;
    private String password;
    private String email;
    private String phoneNumber;

    public String getUserName() {
    
    
        return userName;
    }

    public void setUserName(String userName) {
    
    
        this.userName = userName;
    }

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }

    public String getEmail() {
    
    
        return email;
    }

    public void setEmail(String email) {
    
    
        this.email = email;
    }

    public String getPhoneNumber() {
    
    
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
    
    
        this.phoneNumber = phoneNumber;
    }

    @Override
    public String toString() {
    
    
        return "测试配置结果{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                '}';
    }
}

Test class

@RestController
public class HelloWorldRestController {
    
    
    @Autowired
    private TestConfiguration configuration;
    @GetMapping(value = "/test-config")
    public String testConfiguration(@RequestParam(required = false) String message) {
    
    
        return configuration.toString();
    }
}

Test Results
Insert picture description here

Property automatic assembly one

Set properties configuration test.properties

test.config.userName=sxs
test.config.password=123456
[email protected]
test.config.phoneNumber=176xxxx1573

The rest of the code remains unchanged, and the test execution result is to
Insert picture description here
modify the attribute configuration class

@Configuration
@ConfigurationProperties(prefix = "test.config")
@PropertySource("classpath:/test-config.properties")
public class TestConfiguration {
    
    

Test Results
Insert picture description here

in conclusion

The application.properties file and application.yml file will be automatically loaded in org.springframework.boot.context.config.ConfigFileApplicationListener. The specific description information is as follows:

{@link EnvironmentPostProcessor} that configures the context environment by loading 
properties from well known file locations. By default properties will be loaded from 
'application.properties' and/or 'application.yml' files in the following locations:
加载示例:
<ul>
 <li>classpath:</li>
 <li>file:./</li>
 <li>classpath:config/</li>
 <li>file:./config/:</li>
</ul>

Using @Configuration annotation, plus @PropertySource("classpath:test.properties") annotation, there is no need for any content inside the class, this is a pure configuration loading class. Due to the role of @Configuration (the bottom layer is @Component), it will be scanned by Spring's scanner, loaded into the JVM, and create a Bean, and when it is created, the configuration items in the configuration file will be loaded.

More excellent articles

The code has been updated in GitHub . For more details, please follow dwyanewede .

JDK dynamic proxy implementation principle
https://blog.csdn.net/shang_xs/article/details/92772437
Primary school students in java
https://blog.csdn.net/shang_xs

Official account recommendation

Insert picture description here

Guess you like

Origin blog.csdn.net/shang_xs/article/details/94633717