Spring Boot 自动装配之 ConfigurationProperties

Spring Boot 自动装配之 ConfigurationProperties

Spring Boot特性

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配置

自动装配的过程

  1. 通过各种注解实现了类与类之间的依赖关系,容器在启动的时候Application.run,会调用EnableAutoConfigurationImportSelector.class的selectImports方法(其实是其父类的方法)

  2. selectImports方法最终会调用SpringFactoriesLoader.loadFactoryNames方法来获取一个全面的常用BeanConfiguration列表

  3. loadFactoryNames方法会读取FACTORIES_RESOURCE_LOCATION(也就是spring-boot-autoconfigure.jar 下面的spring.factories),获取到所有的Spring相关的Bean的全限定名ClassName,大概120多个

  4. selectImports方法继续调用filter(configurations, autoConfigurationMetadata);这个时候会根据这些BeanConfiguration里面的条件,来一一筛选,最关键的是
    @ConditionalOnClass,这个条件注解会去classpath下查找,jar包里面是否有这个条件依赖类,所以必须有了相应的jar包,才有这些依赖类,才会生成IOC环境需要的一些默认配置Bean

  5. 最后把符合条件的BeanConfiguration注入默认的EnableConfigurationPropertie类里面的属性值,并且注入到IOC环境当中

ConfigurationProperties属性自动装配

文件类型

配置文件支持yml和properties两种,application.properties文件和application.yml,这两个文件都可以被SpringBoot自动识别并加载,两种文件格式一般被放在src/main/resource目录下面。

除此之外就是自定义配置文件了,一般的格式都是.properties。

属性自动装配一

设置属性配置application.properties

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

属性映射类

@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 + '\'' +
                '}';
    }
}

测试类

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

测试结果
在这里插入图片描述

扫描二维码关注公众号,回复: 12174015 查看本文章

属性自动装配一

设置属性配置test.properties

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

其余代码不变,测试执行结果
在这里插入图片描述
修改属性配置类

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

测试结果
在这里插入图片描述

结论

在org.springframework.boot.context.config.ConfigFileApplicationListener中会自动加载application.properties文件和application.yml文件,具体描述信息如下:

{@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>

使用@Configuration标注,加上@PropertySource(“classpath:test.properties”)注解,类的内部并不需要任何内容,这是一个纯粹的配置加载类。由于@Configuration的作用(底层为@Component),他会被Spring的扫描器扫到,并加载到JVM,并创建Bean,而创建的时候就会执行配置文件中配置项的加载。

更多优秀文章

代码已经在GitHub中更新,更多详情可关注dwyanewede

JDK动态代理实现原理
https://blog.csdn.net/shang_xs/article/details/92772437
java界的小学生
https://blog.csdn.net/shang_xs

公众号推荐

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/shang_xs/article/details/94633717
今日推荐