美文网首页
SpringBoot @Value @Configuration

SpringBoot @Value @Configuration

作者: 毛嘟嘟 | 来源:发表于2021-11-22 15:37 被阅读0次

    前言

    在平时的工作中,我们可能会需要从配置文件***.properties***.yml中获取一些配置参数。那么,在Spring Boot项目中我们可以使用@Value或者@ConfigurationProperties注解来获取。接下来,让我们一起探索这两个注解的详细使用方法吧。

    在接下来的内容中,我们可以学习到如何在配置文件中配置 Key : ValuePOJOList<String>List<POJO>Map<String, String>Map<String, POJO> 结构,并在项目中使用 @Value@ConfigurationProperties 获取这些配置。

    正文

    @Value 与 @ConfigurationProperties 的区别

    spring-boot 官方文档中,我们可以看到这段内容:

    大致内容就是:

    使用@Value("${property}")注解注入配置属性有时会很麻烦,尤其是当您使用多个属性或您的数据本质上是分层的时候。Spring Boot 提供了一种替代方法: @ConfigurationProperties,它让强类型 bean 管理和验证应用程序的配置项。

    文档中还总结了@ConfigurationProperties vs @Value 各自支持的功能特性,如下图:

    感兴趣的朋友可以点击上文中的链接去翻阅官方的详细文档,这里就不做过多介绍了。
    言归正传,下面我们正式开始列举一些常用格式的配置项该如何使用@Value@ConfigurationProperties 去获取。

    1. 配置简单的 Key : Value 结构

    #### properties 文件配置格式:
    app.environments-config.dev-url=https://dev.example.com
    
    
    #### YML 文件配置格式:
    app:
      environments-config:
        dev-url: https://dev.example.com
    
    
    @Value 获取配置
    
    @Value("${app.environments-config.dev-url}")
    private String devUrl;
    
    
    @ConfigurationProperties 获取配置
    @Data
    @Component
    @ConfigurationProperties("app.environments-config")
    public class Config {
        // 获取配置
        private String devUrl;
    
    }
    

    2. 配置 List<String> 结构

      2.1   List<String> - 逗号分隔方式

       2.1.1   properties 文件配置格式:

       2.1.2   YML 文件配置格式:

    从上图中可以看出 @ConfigurationProperties 无需做任何操作就可以自动将以逗号连接的配置项转换为集合;而 @Value 则需要使用 SpEL 表达式来实现。至于为什么 @ConfigurationProperties 可自动实现转换,可以查阅官方文档 Relaxed Binding 这部分内容。

      2.2   List<String> - 数组方式 (仅限于@ConfigurationProperties)

       2.2.1   properties 文件配置格式:

    app.environments-config.dev-url[0]=https://dev.example.com
    app.environments-config.dev-url[1]=https://dev222.example.com
    

       2.2.2   YML 文件配置格式:

    app:
      environments-config:
        dev-url:
          - https://dev.example.com
          - https://dev222.example.com
    

    3. 配置 POJO 结构 (仅限于@ConfigurationProperties)

      3.1   properties 文件格式

      3.2   YML 文件格式:

    4. 配置 List<POJO> 结构 (仅限于@ConfigurationProperties)

      4.1   properties 文件格式

      4.2   YML 文件格式

    \color{#376956}{温馨提示:} 目录 4.1 及 4.2 中不论是 properties文件格式还是 YML文件格式,其中配置的 dev-users 都可使用以逗号分隔的方式来编写,会使配置文件显得更加简洁。

    5. 配置 Map<String, String> 结构

      5.1   @Value 对应的写法

    #### properties文件配置格式:
    app.environments-config={"dev-url":"https://dev.example.com", "qa-url":"https://qa.example.com"} 
    
    
    #### YML文件配置格式:
    app:
      environments-config: '{"dev-url":"https://dev.example.com", "qa-url":"https://qa.example.com"}'
    
    
      // 获取配置 
      @Value("#{${app.environments-config}}")
      private Map<String, String> value;
    
    

      5.2   @ConfigurationProperties 对应的写法

    #### properties文件配置格式
    app.environments-config.dev-url=https://dev.example.com
    app.environments-config.qa-url=https://qa.example.com
    
    
    #### YML文件配置格式
    app:
      environments-config:
        dev-url: https://dev.example.com
        qa-url: https://qa.example.com
    
    
      @Data
      @Component
      @ConfigurationProperties("app")
      public class Config {
          // 获取配置
          private Map<String, String> environmentsConfig;
    
      }
    

    6. 配置 Map<String, POJO> 结构 (仅限于@ConfigurationProperties)

    properties 文件配置方式:
    #### 方式一:
    app.environments-config.dev.url=https://dev.example.com
    app.environments-config.dev.users=zhagnsan,lisi
    
    app.environments-config.qa.url=https://qa.example.com
    app.environments-config.qa.users=xiaobai,xiaohong
    
    #### 方式二:
    app.environments-config.dev.url=https://dev.example.com
    app.environments-config.dev.users[0]=zhagnsan
    app.environments-config.dev.users[1]=lisi
    
    app.environments-config.qa.url=https://qa.example.com
    app.environments-config.qa.users[0]=xiaobai
    app.environments-config.qa.users[1]=xiaohong
    
    YML 文件配置方式:
    #### 方式一:
    app:
      environments-config:
        dev:
          url: https://dev.example.com
          users: zhagnsan,lisi
        qa:
          url: https://qa.example.com
          users: xiaobai,xiaohong
    
    #### 方式二:
    app:
      environments-config:
        dev:
          url: https://dev.example.com
          users:
            - zhagnsan
            - lisi
        qa:
          url: https://qa.example.com
          users:
            - xiaobai
            - xiaohong
    
    @ConfigurationProperties 获取配置
    @Data
    @Component
    @ConfigurationProperties("app")
    public class Config {
        // 获取配置
        private Map<String, EnvironmentsConfigBO> environmentsConfig;
    
        @Data
        public static class EnvironmentsConfigBO {
            private String url;
            private List<String> users;
        }
    
    }
    

    结语:

    通过上面文章的介绍,我们可以看出:在配置简单的 Key : Value 及 List<String> 结构时,可以使用 @Value 去获取会更加方便,其他结构如:POJO、List<POJO>、Map<String, String>、Map<String, POJO> 则使用 @ConfigurationProperties 去获取。


    扩展阅读:

    @Value设置默认值

    // 冒号后面的内容即为默认值
    @Value("${app.environments-config.dev-url:http://localhost}")
    private String devUrl;
    
    

    使用JSR303注解进行有效性校验

    一文学会JSR-303 参数校验,真香

    绑定属性值到第三方jar中包含的Class

    如果需要绑定属性值到第三方jar中包含的Class对象,我们是无法直接在Class上加上@ConfigurationProperties注解的,这时候可以在@Configuration标注的Class中定义一个需要绑定值的Class类型的bean,然后在该方法上加上@ConfigurationProperties。比如下面代码中通过initTestConfigurationProperties()定义了一个TestConfigurationProperties类型的bean,在该方法上加上了@ConfigurationProperties,Spring Boot就会为该bean进行属性值绑定。

    @Configuration
    public class TestConfig {
    
        @Bean
        @ConfigurationProperties("test.config")
        public TestConfigurationProperties initTestConfigurationProperties() {
            return new TestConfigurationProperties();
        }
        
    }
    

    相关链接:

    官方文档
    Spring Boot(07)——ConfigurationProperties介绍

    相关文章

      网友评论

          本文标题:SpringBoot @Value @Configuration

          本文链接:https://www.haomeiwen.com/subject/yikjtrtx.html