🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 69.5 使用YAML配置外部属性 YAML是JSON的一个超集,可以非常方便的将外部配置以层次结构形式存储起来,比如: ```javascript spring: application: name: cruncher datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/test server: port: 9000 ``` 创建一个`application.yml`文件,将它放到classpath的根目录下,并添加`snakeyaml`依赖(Maven坐标为`org.yaml:snakeyaml`,如果你使用`spring-boot-starter`那就已经包含了)。一个YAML文件会被解析为一个Java `Map<String,Object>`(和一个JSON对象类似),Spring Boot会平伸该map,这样它就只有1级深度,并且有period-separated的keys,跟人们在Java中经常使用的`Properties`文件非常类似。 上面的YAML示例对应于下面的`application.properties`文件: ```java spring.application.name=cruncher spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost/test server.port=9000 ``` 查看'Spring Boot特性'章节的[Section 24.6, “Using YAML instead of Properties”](https://github.com/cwiki-us-spring-guides/Spring-Boot-Reference-Guide/tree/0047aa8098a650dde0c93f4d2e91754c83468c4b/IV.%20Spring%20Boot%20features/24.6.%20Using%20YAML%20instead%20of%20Properties.md)可以获取更多关于YAML的信息。