SpringBoot — YAML configuration

Sankara katabathina
2 min readApr 20, 2021

Spring Boot has been very good and easy to handle configuration for the applications. Recently i came across using YAML file for configuration besides .properties file.

Here is example how we can easy map Tree like configuration specified in application.yaml to the POJO and use it very effectively in the code.

Multiple Profiles in single YAML file

we can have multiple profiles one for dev and another for prod with list of configuration values in an single yaml file. and we can enable or disable the environment with a property.

These profiles will be separated by three dash “ — -”. below is the sample dev and prod profiles.

Maven dependencies

We can use springframework aspects module or if we are using JPA module we can skip aspects dependancy

<dependency>
<
groupId>
org.springframework</groupId>
<
artifactId>
spring-aspects</artifactId>
<
version>
3.2.0.RELEASE</version>
</
dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>-->

Annotate Configuration class

Use the below annotation to enable binding the YAML configuration properties with POJO

@Configuration marks the class as a source of bean definitions

@ConfigurationProperties binds and validates the external configurations to a configuration class

@EnableConfigurationProperties this annotation is used to enable @ConfigurationProperties annotated beans in the Spring application

Complex Map and List like configuration

Mapping Tree like configuration to java Map data structure to hold complex objects clear.

below is the snap shot helps Map multiple “sources” to java Map of “source”s object. [ private Map<String, Source> sources; ]

List of srcToDest mapping [ List<SrcToDest> srcToDest; ] in Source class

Full Source code

--

--