Spring - @Value 读取配置属性的三种方式


本文介绍工作中用到的 @Value 读取配置属性的三种方式.
先决条件: 使用 @Value 读取属性值的类对象需要交给 Spring 容器管理

静态变量赋值

直接通过 ApplicationConfiguration.CONTEXT_PATH 的方式即可引用到.
这种方式, 需要引用这个 CONTEXT_PATH 的目标类不需要交给 Spring 容器管理, 也不需要在业务代码中注入配置属性.
目标对象无论是通过 new 还是 @Autowired, 都能获取到这个配置值

@Component
public final class ApplicationConfiguration {

    /**
     * 系统上下文, 形如 /dynamic-authorization
     */
    public static String CONTEXT_PATH;
    
    @Value("${server.servlet.context-path}")
    public void setContextPath(String contextPath) {
        CONTEXT_PATH = contextPath;
    }
    
    private ApplicationConfiguration() {}
}

对象注入方式

值得一提的是, 采用这种方式如果目标对象是通过 new 创建的, 也是无法获取到配置值的.
目标类需要被 Spring 容器管理, 通过以下方式引用:

@Value("${server.servlet.context-path}")
private String contextPath;

@ConfigurationProperties 方式

这种方式要麻烦点,
首先, 需要启用 Annotation Processor;
其次, 我们还是需要一个配置类:
@ConfigurationProperties 的 prefix 指定配置文件里 Key 的前缀, 不可省略. 并且对于 server.servlet.context-path 这种多级属性, 需要用 @NestedConfigurationProperty 标注, 新建一个二级配置对象, 并赋给 ApplicationConfiguration 的成员属性, 这里图省事就写成静态内部类了.
最后同样的, 引用该配置类的目标类需要被 Spring 容器管理, 并且目标类不能用 new 的方式创建.

@Component
@ConfigurationProperties(prefix = "server")
public final class ApplicationConfiguration {
    
    @NestedConfigurationProperty
    private final Servlet servlet = new Servlet();
    
    public Servlet getServlet() {
        return servlet;
    }
    
    public static class Servlet {
        private String contextPath;
        
        public String getContextPath() {
            return contextPath;
        }
        
        public void setContextPath(String contextPath) {
            this.contextPath = contextPath;
        }
    }
    
    private ApplicationConfiguration() {}
}

复杂属性的读取

现在我们在用另外一种方式读取复合配置对象, (利用了 @ConfigurationPropertiesScan). 涉及到了简单 List, 复杂 List 和 复杂 Map 的读取.

配置文件

custom:
  third:
    description: 三方配置
    client-id: a-certain-third-id
    client-secret: a-certain-third-secret
    param:
      - id: 4e80bc144f2507323a348fca15db7c48
        code: MSAD
      - id: fbf501400952c4bb7959d8836693c30f
        code: VBNM
    white-list:
      - 10.10.10.10
      - 10.10.10.11
      - 10.10.10.12
      - 10.10.10.13
    user:
      caplike:
        abbreviation: LiKe
        reg-date: 2021-01-14
server:
  servlet:
    context-path: /value
  port: 15968

接受配置的配置对象

@Data
@ConfigurationProperties(prefix = "custom.third")
public class ThirdConfig {

    private String description;

    private String clientId;

    private String clientSecret;

    private List<Param> param;

    // -----------------------------------------------------------------------------------------------------------------

    private List<String> whiteList;

    @Data
    @NoArgsConstructor
    public static class Param {

        private String id;

        private String code;
    }

    // -----------------------------------------------------------------------------------------------------------------

    private Map<String, UserDetail> user;

    @Data
    @NoArgsConstructor
    public static class UserDetail {

        private String abbreviation;

        private String regDate;

    }

}

启动类

@SpringBootApplication
@ConfigurationPropertiesScan(basePackageClasses = {ThirdConfig.class})
public class ValueApplication {

    public static void main(String[] args) {
        SpringApplication.run(ValueApplication.class, args);
    }

}

测试结果

在这里插入图片描述

Updated.2021-1-14 14:10:14

- END -*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值