DEV Community

Full Stack Hacker
Full Stack Hacker

Posted on

Spring Boot YAML example

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. @ConfigurationProperties annotation allows to access properties set in configuration files (application.properties or application.yml) automatically and powerfully.

YAML and Properties

  • application.yml


logging:
  level:
    org.springframework: ERROR
    com.example: DEBUG

spring:
  profiles:
    active: dev
  main:
    banner-mode: off

email: haunt.hcm2015@gmail.com
thread-pool: 10

wordpress:
  menus:
    - title: Home
      name: Home
      path: /
    - title: About
      name: About
      path: /about
  themes:
    default-folder: /wp-content/themes/toptech
  servers:
    - ip: 127.0.0.1
      path: /dev1
    - ip: 127.0.0.2
      path: /dev2
    - ip: 127.0.0.3
      path: /dev3


Enter fullscreen mode Exit fullscreen mode
  • application.properties ```properties

Spring Boot

logging.level.org.springframework=ERROR
logging.level.com.mkyong=DEBUG
spring.profiles.active=dev
spring.main.banner-mode=off

Global

email=haunt.hcm2015@gmail.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/toptech
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

## Project Structure
![](https://i.imgur.com/Z6dlkHR.png)
## Project Dependency
Spring Boot uses **_SnakeYAML_** library to parse the YAML file, and the **_SnakeYAML_** library is provided by **_spring-boot-starter_**

The library and dependencies are configured in the **_pom.xml_** file

```xml


<?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>


Enter fullscreen mode Exit fullscreen mode

Spring Boot + YAML

Spring Boot will load and parse the YAML file and bind the values in the following @ConfigurationProperties classes.

  • Menu.java


package com.example.yaml.config.model;

public class Menu {
    private String name;
    private String path;
    private String title;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Menu{" +
                "name='" + name + '\'' +
                ", path='" + path + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}


Enter fullscreen mode Exit fullscreen mode
  • Server.java


package com.example.yaml.config.model;

public class Server {
    private String ip;
    private String path;
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public String toString() {
        return "Server{" +
                "ip='" + ip + '\'' +
                ", path='" + path + '\'' +
                '}';
    }
}


Enter fullscreen mode Exit fullscreen mode
  • Theme.java


package com.example.yaml.config.model;

public class Theme {
    private String defaultFolder;
    public String getDefaultFolder() {
        return defaultFolder;
    }

    public void setDefaultFolder(String defaultFolder) {
        this.defaultFolder = defaultFolder;
    }

    @Override
    public String toString() {
        return "Theme{" +
                "defaultFolder='" + defaultFolder + '\'' +
                '}';
    }
}


Enter fullscreen mode Exit fullscreen mode
  • GlobalProperties.java


package com.example.yaml.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class GlobalProperties {
    private int threadPool;
    private String email;
    public int getThreadPool() {
        return threadPool;
    }

    public void setThreadPool(int threadPool) {
        this.threadPool = threadPool;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "GlobalProperties{" +
                "threadPool=" + threadPool +
                ", email='" + email + '\'' +
                '}';
    }
}


Enter fullscreen mode Exit fullscreen mode

The setter (and getter) names must correspond to the property names in the application.properties (or application.yml)

  • WordPressProperties.java


package com.example.yaml.config;

import com.example.yaml.config.model.Menu;
import com.example.yaml.config.model.Server;
import com.example.yaml.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<>();
    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    public Theme getThemes() {
        return themes;
    }

    public void setThemes(Theme themes) {
        this.themes = themes;
    }

    public List<Server> getServers() {
        return servers;
    }

    public void setServers(List<Server> servers) {
        this.servers = servers;
    }

    @Override
    public String toString() {
        return "WordpressProperties{" +
                "menus=" + menus +
                ", themes=" + themes +
                ", servers=" + servers +
                '}';
    }
}



Enter fullscreen mode Exit fullscreen mode

Spring will automatically look for properties placed inside application.properties or application.yml prefixed with wordpress and inject them into the corresponding properties in the WordPressProperties class.

Start a Spring Boot normally and print out the values.

DEMO


Source code

https://github.com/java-cake/spring-boot/tree/main/yaml

Top comments (0)