Latest properties file spring boot yaml

Spring Starting YAML Example – Mkyong.com

yaml-vs-properties

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`

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

  Project Directory

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

menus = new ArrayList <> ();
personal themed themes;
personal listing servers = new ArrayList <> ();

// … getters and setter, toString ()

4.2 Model, nothing special, just some commonplace categories

Menu.java

package deal com.mkyong.config.mannequin;

public class menu

personal language identify;
personal string;
personal string;

// … getters and setter, toString ()

Server.java

package deal com.mkyong.config.model;

public class server

personal string ip;
personal string;

// … getters and setter, toString ()

Theme.java

package deal com.mkyong.config.model;

generic theme

personal String defaultFolder;

// … getters and setter, toString ()

four.three Start normal startup normally and print values.

Software.java

package deal com.mkyong;

import com.mkyong.config.GlobalPerertiesit;
import com.mkyong.config.WordpressProperties;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class The appliance implements CommandLineRunner

@Autowired
personal WordPressProperties wpProperties;

@Autowired
personal GlobalProperties globalProperties;

public Static void primary (String [] args)
SpringApplication.run (Software.class, args);

@Bypass
public void run (String … args)
System.out.println (globalProperties);
System.out.println (wpProperties);

DEMO 5

Package deal and Use

# Run the spring begin immediately
$ mvn spring-boot: run

# or package deal and run it
$ mvn package deal
$ java -jar target / spring-boot-yaml-example-1.0.jar

Energy

GlobalProperties
span = 10,
e mail = & # 39; properties@mkyong.com'

WordPressProperties
menus = [
Menuname=’Home’, path=’/’, title=’Home’,
Menuname=’About’, path=’/about’, title=’About’
],
themes = Theme defaultFolder = & wp-content material / theme / mkyong & # 39;,
servers = [
Serverip=’127.0.0.1′, path=’/dev1′,
Serverip=’127.0.0.2′, path=’/dev2′,
Serverip=’127.0.0.3′, path=’/dev3′
]

Download Source

References