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.
Now the question arises how we set active profile, ie which properties file we want to load.
In application.properties file we set active profile
Now application properties from application-dev will be loaded.
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
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 –