Spring Boot YAML example
In this article, we will show you how to use YAML instead of properties file in Spring Boot.
Tested with :
- Spring Boot 2.1.2.RELEASE
- Maven 3
- Snakeyaml:jar:1.23
In short, create a application.yml in the src/resources folder, Spring Boot will load and parse .yml file automatically and bind the values into the classes which annotated with @ConfigurationProperties
P.S YAML files cannot be loaded by using the `@PropertySource`
1. YAML and Properties
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
# 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=/dev1 wordpress.servers[1].ip=127.0.0.2 wordpress.servers[1].path=/dev2 wordpress.servers[2].ip=127.0.0.3 wordpress.servers[2].path=/dev3
2. Project Structure
3. Project Dependency
Spring Boot uses SnakeYAML library to parse the YAML file, and the SnakeYAML library is provided by spring-boot-starter
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-yaml-example</artifactId> <packaging>jar</packaging> <name>Spring Boot YAML Example</name> <url>https://www.mkyong.com</url> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> </project>
Project Dependencies :
+- org.springframework.boot:spring-boot-starter:jar:2.1.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.1.2.RELEASE:compile [INFO] | | \- org.springframework:spring-context:jar:5.1.4.RELEASE:compile [INFO] | | +- org.springframework:spring-aop:jar:5.1.4.RELEASE:compile [INFO] | | +- org.springframework:spring-beans:jar:5.1.4.RELEASE:compile [INFO] | | \- org.springframework:spring-expression:jar:5.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.2.RELEASE:compile [INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.1:compile [INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.1:compile [INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | +- org.springframework:spring-core:jar:5.1.4.RELEASE:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.1.4.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.23:runtime <<------------- SnakeYAML
4. Spring Boot + YAML
4.1 Spring Boot will load and parse the YAML file and bind the values in the following @ConfigurationProperties classes.
package com.mkyong.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties // no prefix, root level. public class GlobalProperties { //thread-pool , relax binding private int threadPool; private String email; //... getters and setters, toString()
package com.mkyong.config; import com.mkyong.config.model.Menu; import com.mkyong.config.model.Server; import com.mkyong.config.model.Theme; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component @ConfigurationProperties("wordpress") public class WordPressProperties { private List<Menu> menus = new ArrayList<>(); private Theme themes; private List<Server> servers = new ArrayList<>(); //... getters and setters, toString()
4.2 Model, nothing special, just some standard classes.
package com.mkyong.config.model; public class Menu { private String name; private String path; private String title; //... getters and setters, toString()
package com.mkyong.config.model; public class Server { private String ip; private String path; //... getters and setters, toString()
package com.mkyong.config.model; public class Theme { private String defaultFolder; //... getters and setters, toString()
4.3 Start a Spring Boot normally and print out the values.
package com.mkyong; import com.mkyong.config.GlobalProperties; import com.mkyong.config.WordpressProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private WordPressProperties wpProperties; @Autowired private GlobalProperties globalProperties; public static void main(String[] args) { SpringApplication.run(Application.class, args); @Override public void run(String... args) { System.out.println(globalProperties); System.out.println(wpProperties);
5. DEMO
Package and run it.
# run spring boot directly $ mvn spring-boot:run # or package and run it $ mvn package $ java -jar target/spring-boot-yaml-example-1.0.jar
Output
GlobalProperties{ threadPool=10, email='properties@mkyong.com' WordpressProperties{ menus=[ Menu{name='Home', path='/', title='Home'}, Menu{name='About', path='/about', title='About'} ], themes=Theme{defaultFolder='/wp-content/themes/mkyong'}, servers=[ Server{ip='127.0.0.1', path='/dev1'}, Server{ip='127.0.0.2', path='/dev2'}, Server{ip='127.0.0.3', path='/dev3'}
Refer to this Spring Boot + Multiple Profiles YAML example
From:一号门
COMMENTS