ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

SpringBoot配置文件yml

2026/9/25 5:04:09 拓冰建站 浏览量
SpringBoot配置文件yml

一 、使用yml文件配置属性,而非properties文件
environments:
    dev:
        url: https://dev.example.com
        name: Developer Setup
    prod:
        url: https://another.example.com
        name: My Cool App
上面等价下面的properties配置,实际上是会转换成如下的配置:

environments.dev.url=https://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=https://another.example.com
environments.prod.name=My Cool App
二、List 类型的属性配置

my:
   servers:
       - dev.example.com
       - another.example.com
等价如下的properties 配置

my.servers[0]=dev.example.com
my.servers[1]=another.example.com
三、Java 中定义对应配置类
注意要有set方法。

@ConfigurationProperties(prefix="my")
public class Config {
 
    private List<String> servers = new ArrayList<String>();
 
    public List<String> getServers() {
        return this.servers;
    }
    public void setServers(List servers) {
        this.servers = servers;
    }
}
四、如何定义List<List<String>> 的属性
my: 
  clusters: 
    cluster
       - c1_Host1,c1_Host2
       - c2_Host1,c2_Host2

整理自:

SpringBoot 在 yml 文件中配置双层List 属性的方法_yml多层list-CSDN博客