From 568057edbc9e211b061d65fa00275f47b8b7a492 Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Mon, 23 Mar 2015 23:54:05 +0800 Subject: [PATCH] Update 63.4. Use YAML for external properties.md --- .../63.4. Use YAML for external properties.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/IX. ‘How-to’ guides/63.4. Use YAML for external properties.md b/IX. ‘How-to’ guides/63.4. Use YAML for external properties.md index e69de29..f7b2024 100644 --- a/IX. ‘How-to’ guides/63.4. Use YAML for external properties.md +++ b/IX. ‘How-to’ guides/63.4. Use YAML for external properties.md @@ -0,0 +1,23 @@ + +### 63.4. 使用YAML配置外部属性 + +YAML是JSON的一个超集,可以非常方便的将外部配置以层次结构形式存储起来。比如: +```json +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`(和一个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 23.6, “Using YAML instead of Properties”](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-yaml)可以获取更多关于YAML的信息。