> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/24.-externalized-configuration/24.6.-using-yaml-instead-of-properties/24.6.1.-loading-yaml.md).

# 24.6.1. 加载YAML

Spring框架提供两个便利的类用于加载YAML文档，`YamlPropertiesFactoryBean`会将YAML加载为`Properties`，`YamlMapFactoryBean`会将YAML加载为`Map`。

例如，下面的YAML文档：

```javascript
environments:
    dev:
        url: http://dev.example.com
        name: Developer Setup
    prod:
        url: http://another.example.com
        name: My Cool App
```

会被转化为如下属性：

```java
environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool App
```

YAML列表被表示成使用`[index]`间接引用作为属性keys的形式，例如下面的YAML：

```javascript
my:
   servers:
       - dev.example.com
       - another.example.com
```

将会转化到这些属性:

```java
my.servers[0]=dev.example.com
my.servers[1]=another.example.com
```

使用Spring `DataBinder`工具集绑定这些属性（这是`@ConfigurationProperties`做的事）时，你需要确保目标bean有个`java.util.List`或`Set`类型的属性，并且需要提供一个setter或使用可变的值初始化它，比如，下面的代码将绑定之前的属性：

```java
@ConfigurationProperties(prefix="my")
public class Config {
    private List<String> servers = new ArrayList<String>();
    public List<String> getServers() {
        return this.servers;
    }
}
```

**注** 当多个位置配置了列表，重写通过替换整个列表发生。在之前的例子里，当在几个地方定义了`my.servers`，来自`PropertySource`拥有更高优先级的整个列表会重写那个列表的任何其它的配置。可以使用逗号分隔的列表和YAML列表来完全重写列表的内容。
