On this article, we show you the best way to use YAML as an alternative of a location file if you start up in spring.
Examined with:
- Spring Boot 2.1.2.RELEASE
- Maven three
- Snakeyaml: jar: 1.23
Briefly, create an software token in the src / assets folder, obtain Spring Boot routinely parses the .yml file and binds the values to the classes marked with @ConfigurationProperties
. PS YAML information can’t be downloaded using `@ PropertySource`
Contents
1. YAML and Options
software.yml
registration:
degree:
org.springframework: ERROR
com.mkyong: DEBUG
spring:
profiles:
lively: dev
crucial:
banner mode: off
E mail: yaml@mkyong.com
spiral pool: 10
WordPress:
menus:
– Title: Residence
Identify: Residence
path: /
– Title: About
Identify: About
path: / about
themes:
default folder: / wp-content / themes / mkyong
servers:
– ip: 127.zero.0.1
path: / dev1
– ip: 127.0.0.2
path: / dev2
– ip: 127.zero.zero.three
path: / dev3
software.properties
# Spring Start
logging.degree.org.springframework = ERROR
logging.degree.com.mkyong = DEBUG
spring.profiles.lively = dev
spring.foremost.banner-mode = off
# International
e-mail=properties@mkyong.com
yarn pool = 10
# WordPress
wordpress.menus [0] .title = Residence
wordpress.menus [0] .identify = House
wordpress.menus [0] .path = /
wordpress.menus [1] .title = About
wordpress.menus [1] .identify = Info
wordpress.menus [1] .path = / about
wordpress.themes.default folder = / wp-content material / themes / mkyong
wordpress.servers [0] .IP = 127.0.0.1
wordpress.servers [0] .path = / dev1
wordpress.servers [1] .IP = 127.0.zero.2
wordpress.servers [1] .path = / DEV2
wordpress.servers [2] .IP = 127.zero.0.3
wordpress.servers [2] .path = / dev3
2. Undertaking Structure

3. Undertaking Dependence
Spring Start Uses the SnakeYAML Library to Parse the YAML File, and the SnakeYAML Library is Out there with Spring-Boot-Starter
pom.xml
4.0.0
Spring-boot-yaml Example [19659017] jar
Spring Buffer YAML Example
https://www.mkyong.com
1,0
org.springframework.boot
spring-begin host
2.1. 2.RELEASE
1.eight
org.springframework.boot
bow-play starter
org.springframework.boot
bow-play-maven extension
org.apache. maven. plugins
maven-surefire plugin
2.22.zero
Undertaking dependencies:
+ – org.springframework.boot: spring-boot-starter: jar: 2.1.2.RELEASE: flip
[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.four.RELEASE: put together
[INFO] | | – org.springframework: spring-expression: jar: 5.1.four.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-basic: jar: 1.2.3: translate
[INFO] | | | – ch.qos.logback: logback-core: jar: 1.2.three: 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: translate
[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.four.RELEASE: put collectively
[INFO] | – org.yaml: snakeyaml: jar: 1.23: runtime << ————- SnakeYAML
four. Spring Boot + YAML
four.1 Spring Boot downloads and parses the YAML file and binds the values to the next @ ConfigurationProperties classes.
GlobalProperties.java
package deal com.mkyong.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Element;
@Element
@ConfigurationProperties // no prefix, root degree.
basic category GlobalProperties
// yarn pool, loosen up binding
personal int threadPool;
personal String e mail;
// … getters and setter, toString ()
WordPressProperties.java
package deal com.mkyong.config;
convey com.mkyong.config.model.Menu;
import com.mkyong.config.mannequin.Server;
import com.mkyong.config.model.Theme;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Element;
import java.util.ArrayList;
import java.util.Listing;
@Element
@ConfigurationProperties ("wordpress")
public class WordPressProperties
personal listing