Spring Boot 特性(二)外部化配置

2020-05-16  本文已影响0人  镏金糖豆

关于spring读取配置的顺序和读取配置的位置,开发中大多不关注,故此忽略,如有兴趣,请参看源文档。
源文档地址:Externalized Configuration

1. Configuring Random Values

随机配置,基于RandomValuePropertySource ,在配置文件里可以写随机值

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

2. Accessing Command Line Properties

这点告诉我们可以接受命令行参数作为属性,会覆盖aplication配置里的属性。
比如启动命令加上 --server.port=9000

3. Application Property Files

Spring加载配置文件的位置,按顺序从以下四个次序:

4. Profile-specific Properties

不同环境应用不同的配置,通过 application-{profile}.properties,应该都很熟悉了。
目前大多数项目都使用配置中心实现这个功能,了解一下可以了。

5. Placeholders in Properties

就想下面这样,用占位符取属性。

app.name=MyApp
app.description=${app.name} is a Spring Boot application

6. Encrypting Properties

spring没有提供加密属性的方法,但是提供了 EnvironmentPostProcessor 接口,可以在启动完成前环境准备之后更改属性。这个接口显然不能通过bean的方式配置,需要通过SPI的方式加载。

7.Using YAML Instead of Properties

可以使用yaml作为配置文件,这个也司空见惯了,基本都是这样子用的。

8. Type-safe Configuration Properties

即通过@ConfigurationProperties 直接注入一组属性到配置类中,比起@Value注解,对于层次化的属性来说更为友好。无论做架构还是业务代码都很常用,重点了解。

@ConfigurationProperties("acme")
@Component
public class AcmeProperties {
    @Getter
    @Setter
    private boolean enabled;
    @Getter
    @Setter
    private InetAddress remoteAddress;
    @Getter
    private final Security security = new Security();

    @Getter
    @Setter
    public static class Security {
        private String username;
        private String password;
        private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

    }
}
# application.yml

acme:
    enabled:true
    remote-address: 192.168.1.1
    security:
        username: admin
        roles:
          - USER
          - ADMIN

# additional configuration as required
@ConfigurationProperties(prefix = "another")
@Bean
public AnotherComponent anotherComponent() {
  ...
}
@ConfigurationProperties(prefix="acme.my-project.person")
public class OwnerProperties {
    private String firstName;
}

属性绑定并不十分严格,支持各种风格的写法。
对于上面这个类,一下四种都可以注入 firstName。

//推荐 .properties .yml
acme.my-project.person.first-name
//传统 camel 
acme.myProject.person.firstName
acme.my_project.person.first_name
//推荐系统环境变量的写法
ACME_MYPROJECT_PERSON_FIRSTNAME

不同属性源的绑定规则:

Property Source Simple List
Properties Files a_b 或者 a-b 或者 aB a.b[1]或者,
YAML Files a_b 或者 a-b 或者 aB -或者,
Environment Variables A_B A_B_1
System properties a_b 或者 a-b 或者 aB a.b[1]或者,

Map绑定:使用[]才会保留所有字符,否则会去掉特殊字符

acme:
  map:
    "[/key1]": value1  => /key1
    "[/key2]": value2  => /key2
    /key3: value3      => key3 
@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {
    @NotNull
    private InetAddress remoteAddress;
    @Valid
    private final Security security = new Security();
    // ... getters and setters
    public static class Security {
        @NotEmpty
        public String username;
        // ... getters and setters
    }

}
Feature @ConfigurationProperties @Value
Relaxed binding Yes Limited (see note below)
Meta-data support Yes No
SpEL evaluation No Yes

ps:关于 Meta-data,原文介绍如下:

Spring Boot jars include metadata files that provide details of all supported configuration properties. The files are designed to let IDE developers offer contextual help and “code completion” as users are working with application.properties or application.yml files.
只有ide开发人员才会获取meta data,基础框架建设者可以提供ide识别的json数据,普通程序员还是洗洗睡吧。

上一篇 下一篇

猜你喜欢

热点阅读