Spring Boot 配置文件 YAML 共享变量

Spring Boot YAML About 1,060 words

共享变量

定义的配置,有多个地方需要使用,避免重复配置

锚点 & 和引用 *

锚点&定义变量,引用*使用变量。

案例

${info.name}:字符串类型配置可以使用Spring${}表达式来引用配置的值。

&email test@test.com:字符串类型配置使用&定义加配置的值表示&email的变量值为test@test.com

<<: *phone:表示将keyvalue都作为变量。private.phone中的字段为phone1phone2

address: &address:定义整个list为变量。

info:
  name: zhangsan
  email: &email test@test.com
  phone: &phone
    phone1: 18888888888
    phone2: 16666666666
  address: &address
    - a
    - b
    - c

private:
  name: ${info.name}
  email: *email
  phone:
    <<: *phone
  address: *address

Bean 类

info

@Data
@ToString
@Component
@ConfigurationProperties(prefix = "info")
public class BaseInfo {

    private String name;

    private String email;

    private Map<String,String> phone;

    private List<String> address;

}

private

@Data
@ToString
@Component
@ConfigurationProperties(prefix = "private")
public class PrivateInfo {

    private String name;

    private String email;

    private Map<String,String> phone;

    private List<String> address;

}
Views: 1,182 · Posted: 2022-09-18

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh