69.5 Use YAML for external properties
YAML is a superset of JSON and as such is a very convenient syntax for storing external properties in a hierarchical format. E.g.
spring: application: name: cruncher datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/test server: port: 9000
Create a file called application.yml
and stick it in the root of your classpath, and also add snakeyaml
to your dependencies (Maven coordinates org.yaml:snakeyaml
, already included if you use the spring-boot-starter
). A YAML file is parsed to a Java Map<String,Object>
(like a JSON object), and Spring Boot flattens the map so that it is 1-level deep and has period-separated keys, a lot like people are used to with Properties
files in Java.
The example YAML above corresponds to an application.properties
file
spring.application.name=cruncher spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost/test server.port=9000
See Section 24.6, “Using YAML instead of Properties” in the ‘Spring Boot features’ section for more information about YAML.