Enterprise Java

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.

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 folllows :

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;

/**
 * @author javaSolutionsGuide
 *
 */
@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 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 browser ,we should get “This is a local Environment”.

2.6 Let us start the application again by clicking on 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 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 default profile which is none other than the default application.properties file.

Note : If you want you can change 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 DEV environment,so that you want your application to pick DEV specific properties when application is running on DEV and LOCAL environment specific properties when  application is running  on local.

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

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

2.8 Let us add following line in 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 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

Which shows that dev profile is active now.

2.11 Let us test actually and see if our controller picks 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 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 properties file in 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 environment variable in eclipse.

spring.profiles.default  = dev

Now,if you will re-run your application,you will see 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 above logs that dev is now treated as 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 default profile,by default spring boot will pick configurations from application.properties file.If you want to make any other environment configurations as default,you can set spring.profiles.default property to that environmenta 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.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to use profiles in Spring Boot Application

Opinions expressed by Java Code Geeks contributors are their own.

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
minuu.kang
4 years ago

Hi Gaurav,
Thanks for your document and this is what I’ve found.

I’ve tried it but it doesn’t work as we expected.
I created a question in Stackoverflow and if you don’t mind , Could you look into it ?
(link :https://stackoverflow.com/questions/58416384/although-the-spring-profile-default-is-set-up-dev-the-dev-profile-is-not/58416545?noredirect=1#comment103177340_58416545)

Thanks for your appreciate

Back to top button