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:

1

2

3

4

5

application-{profile}.properties

OR

application-{profile}.yml

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:

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

1

2

3

4

5

6

7

8

9

10

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

Define profiles based Properties

Multi profiles .properties example.

application.properties

1

2

#spring profile

spring.profiles.active=dev

application-dev.properties

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#dev environment

#Logging

logging.level.org.springframework.web=ERROR

logging.level.com.javabycode=DEBUG

logging.level.=ERROR

#spring

spring.main.banner-mode=off

#server

server.email:dev@javabycode.com

server.ip=127.0.0.1

server.path=/localhost

application-prod.properties

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#production environment

#Logging

logging.level.org.springframework.web=ERROR

logging.level.com.javabycode=ERROR

logging.level.=error

#spring

spring.main.banner-mode=off

#server

server.email:prod@javabycode.com

server.ip=192.168.0.1

server.path=/javabycode.com

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