Profiles in Spring Framework :

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments like Prod, Dev and QA and any class having @Component or @Configuration can be marked with @Profile

Lets create a spring boot sample projects to demonstrate profiles in spring framework…Here i am using Spring Initializr to create the project :

Now click on GENERATE and it will start the download and will be downloaded as a zip file now extract it and import this maven project in your favorite ide here i am using STS as an IDE

Now lets create three schema in our database i am using Mysql as database so local, dev and prod are three schemas which i have created in Mysql workbench.

Now open the application.properties file and provide the database related information :

application.properties :

Now create two more properties files in src/main/resources for dev and prod environments separately like this : application-dev.properties

and application-prod.properties and provide the database related details :

application-dev.properties :

application-prod.properties :

Now create a model class : Here is a User class for demonstration :

Now create a dao or repository to fetch details from DB:

Next step is to create a service layer class :

Here You can see i have used @Profile and provided all the different evironments to access the data so i can use any of these environments to fetch data from the databases :

Now lets create a controller class to test the data using postman :

Now open the main or root application.properties file :

So here you can see the profile which im using is local which is configured using spring.profiles.active=local so it will fetch data from local database which we created earlier in this section..

So now lets go to the postman and hit the endpoint but before that lets insert some data in these three databases so for demo purpose i have inserted some rows in these databases which are :

local db
proddb
devdb

Now in main application.properties file we are using profile as local

now start the spring boot application so when we will hit the endpoint http://localhost:8080/api/v1/users it will show you all the users from local db :

Now lets change the profile in application.properties to prod

spring.profiles.active=prod now start the spring boot application and when you will hit the same endpoint http://localhost:8080/api/v1/users then it will show you all the users from prod db

Simillarly now lets change the profile in application.properties to dev spring.profiles.active=dev now start the spring boot application and when you will hit the same endpoint http://localhost:8080/api/v1/users then it will show you all the users from dev db

So you can see here by switching the profiles we are able to fetch data from different schemas..

Keep Learning…

--

--

Engineer | Blogger | Traveller

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store