Search⌘ K
AI Features

Spring Boot Profiles

Explore how to define and activate Spring Boot profiles to customize application behavior for various deployment environments. Understand environment-specific configurations including server ports and properties, and learn multiple methods to activate profiles effectively for enterprise applications.

Introduction

Regardless of what enterprise application you’re developing, it will have to adapt based on the environment it’s deployed on. . The behavior of the application should change based on the environment in which it is deployed like: Dev | Test | Stage | Prod | UAT / Pre-Prod. Further one of the 12 Factor principles says “Treat backing services like DB or messaging systems as attached resources, not an integral part of our application. So that it can be attached, detached, or changed without disrupting business logic.”

Each environment requires a setting that is specific to them. For example, in DEV, we don’t really need a licensed enterprise-grade database but for UAT, Pre- prod, or prod we definitely need something like Oracle/DB2, etc. The above problem can be solved in many ways without any framework. Here,Spring Boot takes care of boilerplate code and provides box capability.

Let’s try to solve a simple problem: We want our application to run on a different port in a different environment.

Spring boot profile?

In spring we maintain profiles with the help of properties or .yml files because it contains configuration external to the application.

The default profile

Even if we don’t have a profile for our application, Spring Boot always creates one Default profile. Below is a printed log from application startup.

2021-03-07 16:25:15.927  INFO 6264 --- [           main] com.club.eliteclub.EliteClubApplication  : No active profile set, falling back to default profiles: default

Notice that the default profile is always active. Spring Boot loads all properties from application.yml/application.properties into the default profile

Environment specific profiles

How do we create different profiles in Spring Boot?

Simple - Property/yml Files

To create different profiles for our application, we only need a property file with the naming convention below:

application-{profile_name}.properties

For example, our application has profile local, QA and prod. In that case, we need three more property files in addition to the default application.properties file

1. application-local.properties
2. application-QA.properties
3. application-prod.properties

Modify server port

By default, the Spring Boot application runs on port 8080 but we can use a different port for different environments. If we have created those files already next step is to add a property server.port in each property file and set the value as below:

Environment Port
local 8080
QA 8081
prod 9090

Now that we have a profile-specific configuration, the next obvious question is how to activate a specific profile. Let’s see the activation process.

How to activate profiles?

Now that we have multiple profiles we need to utilize one based on the environment in which the application is deployed. We need to tell the spring boot which profiles to use so that the spring boot can load the correct property file.

It requires profile activation.

Spring only acts on a profile if it is enabled. Otherwise, it falls back to the default one. Let’s look at the different ways to activate a profile.

Using property

If we have a default application.properties file in our classpath then profile activation can be done by setting a property. Setting the below property will activate the dev profile.

spring.profiles.active=dev.

After starting the application we can clearly see the profile activated from the log statement

2021-03-11 12:50:42.249  INFO 2960 --- [           main] com.club.eliteclub.EliteClubApplication  : The following profiles are active: dev

Using java system property

We can activate the profile using the Java system property:

spring.profiles.active={profilename}

When running the application from the command line using generated fat JAR, the Java system property can be supplied using a -D parameter like below

java -Dspring.profiles.active=dev-jar eliteclub-0.0.1-SNAPSHOT.jar

Using env variable - Preferred approach

In the same way, we can set an environment variable

spring.profiles.active={profilename}

If we don’t know how to set the env variable then we can take the help from the link.

Summary

In this lesson, we discussed how to define a profile with spring & different ways to activate a profile.

Finally, we validated our understanding with an example of different ports for different env based on the spring boot profile.