Spring Boot Profiles YAML Properties example

The Spring Boot Profiles YAML Properties example shows you how to use Spring Profiles based on YAML and Properties. Spring Profiles help developers to create multiple configurations details for different environments. In Spring Boot, the default profile is default, we can set the profile via spring.profiles.active property.

In Spring Boot, we define profiles based on .properties or .yaml files following the syntax:

Let’s begin to create Spring Boot Profiles YAML Properties example:

Project Structure
In this Spring Boot Profiles Yaml Properties example, we are creating a project with the following structure:

Spring Boot Profiles YAML Properties example

Maven dependency
In this Spring Boot Profiles Yaml Properties example, we are using two dependencies: spring-boot-starter and spring-boot-starter-test

Define profiles based Properties

Multi profiles .properties example.

application.properties

application-dev.properties

application-prod.properties

Define profiles based YAML
Multi profiles .yml example. In YAML, we can create multiple profiles by using a “—” separator.

application.yml

Load resource properties using @ConfigurationProperties
Read the properties or yaml files later.

Spring Boot application
Don’t forget to create the Spring Boot Application.

Demo Spring Boot Profiles YAML Properties example

Go to the project directory, we package the jar file using the following command line.

Run the jar file and see output on the console

As you see the output above, our application is using the dev profile.

Now we try to change the profile via the spring.profiles.active parameter like below

Note: This demonstrates for the .yml file. Thus, the properties file is renamed to avoid conflicts. If you want to test .properties file, please rename it back to the correct syntax.

That’s all on the Spring Boot Profiles YAML Properties example.

References
Spring Boot – Profile-specific properties
Spring Boot Profiles Example

Download complete source code, click link below

spring-boot-profiles-yaml-properties.zip (20 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons