What is Spring boot Profiles

Wonder how we will manage different properties file for a different stage of deployment for application? There is a concept called profiling, In below article, we will learn about profiles and how spring boot used profiles in spring boot application.

When we run our Spring Boot Application, below line is seen in a console when an application starts up

No active profile set, falling back to default profiles: default

Have you ever wondered what is profile, I got inquisitive to know about it.

Spring Boot has a concept of profiles that allows having a different configuration for a different environment. Before jumping to real-time use case I would like to share an analogy.

Spring Boot Profiles is similar to polymorphism in java or a human being that will respond differently in different conditions.




Example – I will wear clothes differently based on environment, if its home-casuals, if its office than formals while if its party it will be partywear.
Similarly, when we develop our application our environment will be different.

Another Example while developing an application we may want to run on port 8080 and with an embedded database.

But in production, we need to run an application on port 8081 as 8080 is already in use and we have to use mysql db.

Having one property file, will make job tedious as we need to edit a property file based on the environment.

What if we could have different properties file based on the environment, eg in the above case one for development and other for production this is exactly what spring boot provides.

How to Set up Profiles:

For simplicity lets consider that our requirement is to run our application on port 8081 in a dev environment and port 8082 in production, how will we do this.

Create 2 properties file in same location src/main/resources folder as application.properties.

application-dev.properties

server.port=8081

application-prod.properties

server.port=8082

Now the question arises how we set active profile, ie which properties file we want to load.

Approach 1:

In application.properties file we set active profile

application.properties

spring.profiles.active=dev

Now application properties from application-dev will be loaded.

Approach 2:

We select Run as, run configuration instead of running appliction as a java application

Run as->Run Configuration ->Arguments->VM Arguments

and then set active profile

-DSpring.profiles.active=dev

In both approaches, once the application starts up , we will see the below line in the console

The following profiles are active: dev




I hope that this articles adds some value to your Spring Boot development. If you face any issue in setting up a profile for spring boot application, leave us a comment.

Some Useful Links –

Global Exception Handling for Rest Controller in Spring boot

Hello World application in spring boot using controller

You may also like...

1 Response

  1. anil goyal says:

    this a new learning for me

Leave a Reply

Your email address will not be published. Required fields are marked *