Spring Boot

Spring Boot Profiles with examples

In this tutorial, we are going to look at spring boot profiles and what is the use of it?

Profiles are the core feature of spring boot framework that provides a way to segregate parts of your application configuration and make it available only in a certain environment.

It is highly useful when configuring a different environment like prod, dev, test, etc.

What is the use of profiles in Spring?

Before I demonstrate how to manage spring profiles, let’s take a look where spring profiles are useful?

Let’s take an example of an e-commerce application.

In a real e-commerce application, there could be millions of products stored in the database.

So, how you can ensure that while adding features or while doing testing there won’t be any modification of existing data.

Here spring profiles are extremely useful.

By using spring profiles you can set different environments like production (prod), development (dev) and testing.

And you can set different database with dummy values for dev and testing environment.

You can also set different configuration properties like server port, data source, AdSense publisher id for different environments.

Let’s understand spring profiles using a demo project.

Time needed: 15 minutes.

We will follow three steps to create a spring boot profiles demo :

  1. Go to Spring Initializr and create a new project

    Create a new project ‘springboot-profiles-demo’ and select ‘spring-web-starter’ dependency.Spring Boot Profiles

  2. Create three different environment profiles for prod, dev, and test

    We can create separate configuration files by creating configuration files or ‘yml’ property files.spring boot profiles yml file

  3. Choose which profile you want to activate

    There are multiple ways to activate a profile. For example, you can activate prod, dev, or test profile by setting up VM args or Run Configuration –> Arguments.

How to create environment profiles

There are two ways to create Profiles. Let’s take a look at both of them.

Create a Spring boot profile by using the yml file.

Spring Profiles with YML

To create a profile using yml, you can go to main->resources and create two files application-dev.yml for dev environment and application-test.yml for test environment

Now let’s set up a default server port in the application.properties file

Let’s make some changes in application-dev.yml and application-test.yml to change port details for both dev and test environment.

As you can see in the above code, our application will use the 8090 port when the dev environment is active however it will use 7070 port if the test environment is active.

On the contrary, what will happen when no specific environment is active?

Then the application will run in the 9000 port which is default specified in application.properties.

Logs when ‘dev’ profile was active

When ‘test’ profile was active

Logs when the ‘dev’ profile was active

Use @Profile on a configuration class.

If you want spring container to process configuration class or any bean class (service/component/repositories) only if a particular profile is active then you can use @Profile annotation.

Let’s have a look at below TestConfigProfile class

The spring container will process the above class only if the ‘test’ profile is active.

Scenario -1 When the ‘dev’ profile is active.

Scenario – 2 When the ‘test’ profile is active.

As you could see in the above example, when the ‘test’ profile was active, then only spring container processed TestConfigProfile and log 'Test environment loaded' was printed to the console.

How to activate a Profile

There are multiple ways to activate a profile. Let’s take a look at them one by one.

New environment variable in the run configuration

This is the most convenient way to activate a profile.

spring profile active

To activate a profile you can create a new environment variable spring.profiles.active in the Environment tab. and provide the value ‘dev’.

VM Arguments

You can also provide VM arguments in the run configuration to activate a profile. As you could see below logs, the same profile ‘dev’ activated during application start up.

vm arguments to active profiles


In this tutorial, we discussed how to set up and activate profiles in Spring boot. Also, I’ve demonstrated the spring profiles concept using a few real-world examples. In case of any doubts, you can ask me anything in the comments section.