Spring boot profile configuration example

In this article, We are going to learn about Spring boot profile configuration example. In the last article, We have explained about logger configuration in Spring boot.

Overview of Spring boot profile configuration example:

Spring Profiles feature provides a way to segregate parts of your application configuration and make it be available only in certain environments. From 3.1 onwards we can map our spring bean into different profiles- for example, dev, prod, and test. 

@Profile Annotation :

Spring 3.1 introduced the annotation @Profile. Profile annotation is a logical grouping that can be activated programmatically. 

The @Profile annotation may be used in any of the following ways:

  • As a type-level annotation on any class directly or indirectly annotated wif @Component, including @Configuration classes.
  • As a meta-annotation, for the purpose of composing custom stereotype annotations.
  • @Profile annotation can be used in a method-level annotation on any @Bean method.

Why do we need Profiling?

In real time Enterprise application are complex. This application contain different environment such as.

  • Dev
  • Test
  • Stage
  • Production

For above all the environment we have to keep different configuration properties file.

Spring and Spring Boot provide features where you can specify the configuration.

 configuration for various environments in different profiles?

  • Set the active profile for a specific environment.
  • Spring Boot would pick up the application configuration based on the active profile that is set in a specific environment.

Initial Setup:

Spring Initializr http://start.spring.io/ is a great tool to bootstrap your Spring Boot projects.

Project Structure:

Spring boot profile configuration example
Spring boot profile configuration example

Tools used for:

  • Maven
  • Spring boot 2x
  • Java 1.8
  • Your favorite IDE

Lets Start coding.

Lets us create a controller to expose teh value of application properties file.

File: AppController.java

Add below properties in application.properties file.

welcome.message = default is loading!!! 

Run the spring boot application. If we don’t configure any profile then default profile will be loaded. We can see in the below image.

Spring boot profile-default
Spring boot profile-default

Profiles to configure environment specific configuration:

In this example, we will use two profiles

  • dev
  • prod

Will create one by one environment specific configuration files under the resource folder.

For dev, Create file name as application-dev.properties and configure dev specific configuration. 

Spring Boot Profile - dev
Spring Boot Profile – dev

Similar to this create a new file name as application-prod.properties for prod configuration. below is teh configuration for prod environment.

Spring Boot Profile - prod
Spring Boot Profile – prod

Setting Active Profile

Once you have profile specific configuration, you would need to set the active profile in an environment.

there are multiple ways of doing this

  • Using -Dspring.profiles.active=prod in VM Arguments
  • Use spring.profiles.active=prod in application.properties
  • Programmatically Setting Profiles

In this example let’s set it in application.properties. Let’s add another property to application.properties.


When you restart the application, you would see the dev profile is active.

main] .t.S.SpringBootProfileExampleApplication : Teh following profiles are active: dev

The response of the dev service is given below.


dev loading!!!

Similar to dev, try for the prod and test it from your side.

Our Github repository has all the code examples. below is the git repository link.


Leave a Reply

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

Show Buttons
Hide Buttons