How to use profiles in Spring Boot Application

Hello Friends,

In this tutorial,we will learn,how we can use profiles in a Spring Boot Application.




We will discuss following points in this tutorial :

1. What is Spring Boot Profile and Why we need Profiling
2. How to do Profiling in Spring Boot with Example
3. How to set/change the default profile

1. What is Spring Boot Profile and Why we need Profiling

Suppose,you are working on a Spring Boot Application.You have tested application locally on your machine by connecting to local database which is installed on your machine.Now you want to deploy this application on DEV environment and you have DEV database server as well where you have your database.

Now while testing the application locally,in your application.properties file,you would have put details like database url,username,password which is for the local database which is installed on your machine,but once you move to DEV environment,you would like your application to talk to DEV database rather than local database.

So what you can do is,you can change application.properties file with the details which are required to connect to the DEV database,commit the code and deploy it on DEV,but the problem now is this code will connect fine with DEV database but when you will try to execute this code from local,it would not work,because you have changed database details to DEV database.

So again to make it work on your local,you will have to make changes in the application.properties which are required for local and execute the application.

As you can see,there is lot of hustle involved here in shuffling between local and DEV.

Now Imagine you have more environments like ST,ET(QA),PROD and you have to make changes manually all the times.It will be real nightmare.

So what is the Solution?

Spring Boot Profiles in Rescue!

Spring Boot lets you externalize your application configuration so that you can work with same application code in different environments without you need to make changes.

Spring Boot Profiles allow you to configure multiple application.properties file, per environment,so that when you are on local ,it will use local properties file,when you are on DEV it will use DEV properties file and so on ,without you as a programmer need to make any explicit changes in the code.

So in general,if you some application properties which vary per environment,you can handle that with the help of Spring Profiles.

Looks cool. Isn't it :)

2. How to do Profiling in Spring Boot with Example

 2.1 Follow my  post How to Create a Spring Boot Project with Spring Initializer and create a Spring Boot project with name "Springbootprofiles" .Add only web dependency,as that will be sufficient for our testing.

2.2 In the application .properties file that has been automatically created by Spring intializer,add following line :
application.environment=This is a local Environment

2.3 Run the application by clicking on project and selecting Run as ->Run Configurations -> Run

2.4 Check console logs generated by Spring boot

You will see following line in the logs 
2019-07-07 20:00:52.147  INFO 15560 --- [main] c.b.j.s.SpringbootprofilesApplication    : No active profile set, falling back to default profiles: default

Which basically is telling that we have not set any profile explicitly, so spring boot is using default profile, which in other words mean that Spring boot is using configurations from application.properties file.

How can we check that?

Let us see in the next steps.

2.5 Create a controller with name ProfileController.java as follows :
package com.blogspot.javasolutionsguide.springbootprofiles.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/v1")
@RestController
public class ProfileController {

    @Value("${application.environment}")
    private String applicationEnv;

    @GetMapping
    public String getApplicationEnv() {
        return applicationEnv;
    }
}

Here basically, what we are trying to do is, we are trying to access the application. environment property defined in the application.properties file in our controller using @Value annotation so that when we will hit this resource URI from the browser, we should get "This is a local Environment".

2.6 Let us start the application again by clicking on the project and selecting Run as ->Run Configurations -> Run and then actually hit the resource URI(http://localhost:8080/v1) and see if it is returning the expected value from the application.properties file.


So IT IS working as expected.

Take Away: When we don't set any profile in our spring boot application, by default it picks the default profile which is none other than the default application.properties file.
Note: If you want you can change the default profile to some other properties file as well. We will see that later in this tutorial.

2.7 Now say you want to deploy your code to the DEV environment so that you want your application to pick DEV-specific properties when the application is running on DEV and LOCAL environment-specific properties when the application is running on local.

For that, what we need to do is create another properties file with the name application-dev. properties. In general naming convention is application-{profile name}.properties'

Where profile name is generally an environment name, but it can be any text.

2.8 Let us add the following line in the application-dev. properties file
application.environment=This is a dev Environment

2.9 Now how to tell the application to use dev profile instead of default profile.

For that, we need to set "spring.profiles.active" environment variable  as below :
spring.profiles.active = dev

For setting that ,right click on project,select Run as -> Run Configurations-> Environment->New -> Add Name as spring.profiles.active and Value as dev -> click ok -> Run

2.10 If you will check logs now, you will find the following line in the logs :
2019-07-07 20:22:08.557  INFO 17172 --- [ main] c.b.j.s.SpringbootprofilesApplication    : The following profiles are active: dev
This shows that the dev profile is active now.

2.11 Let us test actually and see if our controller picks a value from application-dev. properties

2.12 Hit the resource URI(http://localhost:8080/v1) and see the result on the browser



And it is clear that this time value has been picked from the application-dev. properties file.

Take Away: We can have n number of properties files in our spring boot application for n number of environments, which will have configurations specific to that environment. All we have to do to use the properties file in the respective environment is to set spring. profiles. active property to that environment and spring boot will pick respective properties file.

3. How to set/change the default profile

As we saw above, by default, spring boot picks the default profile which means it picks application.properties file.What if, instead, we want to make dev as our default profile.

In that case, all you need to do is remove spring.profiles.active and set spring. profiles.default property to the profile which we want to set as default profile as an environment variable in eclipse.
spring.profiles.default  = dev

Now, if you will re-run your application, you will see the following line in your console logs :
2019-07-07 20:35:23.587  INFO 16832 --- [main] c.b.j.s.SpringbootprofilesApplication    : No active profile set, falling back to default profiles: dev
So it is clear from the above logs that dev is now treated as the default profile.

We can verify further by hitting our resource URI(http://localhost:8080/v1)


Take Away: If we don't set any profile as the default profile, by default spring boot will pick configurations from the application.properties file. If you want to make any other environment configurations as default, you can set spring. profiles.default property to that environment and
spring-boot will pick that environment specific property even when spring.profiles.active is not set.

Thanks for reading. Please share it with someone, you think this might be helpful.


Also, if you like JavaSolutionsGuide and want to contribute, go ahead and

- Write an article
- Share with me at javasolutionsguide@gmail.com and
- See Your Article Getting Published on the main Page of JavaSolutionsGuide with your name.