![yaml-vs-properties](https://i-blog.csdnimg.cn/blog_migrate/330827cc2a3e68b6ed541e245481294b.png)
在本文中,我们将向您展示如何在Spring Boot中使用YAML代替属性文件。
经过测试:
- Spring Boot 2.1.2发布
- Maven 3
- Snakeyaml:jar:1.23
简而言之,在src/resources
文件夹中创建一个application.yml
,Spring Boot将自动加载并解析.yml
文件,并将值绑定到用@ConfigurationProperties
注释的类中。
PS YAML文件无法通过使用@PropertySource加载
1. YAML和属性
application.yml
logging:
level:
org.springframework: ERROR
com.mkyong: DEBUG
spring:
profiles:
active: dev
main:
banner-mode: off
email: yaml@mkyong.com
thread-pool: 10
wordpress:
menus:
- title: Home
name: Home
path: /
- title: About
name: About
path: /about
themes:
default-folder: /wp-content/themes/mkyong
servers:
- ip: 127.0.0.1
path: /dev1
- ip: 127.0.0.2
path: /dev2
- ip: 127.0.0.3
path: /dev3
application.properties
# Spring Boot
logging.level.org.springframework=ERROR
logging.level.com.mkyong=DEBUG
spring.profiles.active=dev
spring.main.banner-mode=off
# Global
email=properties@mkyong.com
thread-pool=10
# WordPress
wordpress.menus[0].title=Home
wordpress.menus[0].name=Home
wordpress.menus[0].path=/
wordpress.menus[1].title=About
wordpress.menus[1].name=About
wordpress.menus[1].path=/about
wordpress.themes.default-folder=/wp-content/themes/mkyong
wordpress.servers[0].ip=127.0.0.1
wordpress.servers[0].path=/dev