Configuring Spring Boot with YAML

Spring Boot applications support YAML as an alternative to the application.properties file. YAML isn’t a markup language but it allows you to define properties in the hierarchical configuration.

The parser for YAML is called SnakeYAML and it is automatically added to the classpath by spring-boot-starters so you don’t have to configure anything extra to support YAML is quite convenient if you use a hierarchical configuration data. Spring Boot properties are organized in groups, for example, server, database, and so on.

Project Setup:

  1. Create a new Spring Boot project using your preferred IDE or Spring Initializr.
  2. Within the src/main/resources directory, create a file named application.yml.

In the application.yml file include the following configuration:

spring:
  main:
    banner-mode: off

application:
  servers:
    -   ip: '127.0.0.1'
        path: '/path1'
    -   ip: '127.0.0.2'
        path: '/path2'

Here is an explanation:

spring: Configuration related to Spring itself.

  • main: Disables the Spring Boot banner on startup.

application: Custom application configuration.

  • servers: Defines an array of servers with their IP addresses and paths.

Let’s see how you can read the servers configuration from your code:

@SpringBootApplication
public class DemoApplication {

	@Value("${application.servers[0].ip}")
	private String server1Ip;

	@Value("${application.servers[1].path}")
	private String server2Path;

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Bean
	CommandLineRunner myMethod() {
		return args -> {
			System.out.println("Application Property: " +email);
			System.out.println("server1Ip: " +server1Ip);
			System.out.println("server2Path: " +server2Path);


		};
	}
}

Using YAML configuration for defining profiles

The main advantage of a YAML file is that it can contain configuration for multiple profiles in a single YAML file. Spring Boot provides a spring.profiles key to indicate when the document applies. Let’s see the following example of how to define aprofile-specific configuration in a single YAML file:

spring:
  profiles:
    active: dev

Use Cases for YAML Configuration:

While both YAML and properties files can store configuration data, here are some scenarios where YAML might be preferred:

  • Readability: YAML supports indentation and nested structures, which can make complex configurations easier to read and maintain compared to flat key-value pairs in properties files.
  • Hierarchical Data: When dealing with hierarchical configurations nested within each other, YAML’s nested structure provides a more natural representation.
  • Complex Data Types: YAML can represent complex data types like lists of maps more easily than properties files.
  • Version Control: YAML files are more human-readable within version control systems, making configuration changes easier to track.

However, properties files still have their place:

  • Simplicity: Properties files are simpler for basic configurations with key-value pairs.
  • Legacy Systems: Some legacy systems might have existing tooling that works better with properties files.

Ultimately, the choice between YAML and properties files depends on the complexity of your configuration and your team’s preferences.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon