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 :
- Go to Spring Initializr and create a new project
Create a new project ‘springboot-profiles-demo’ and select ‘spring-web-starter’ dependency.
- Create three different environment profiles for prod, dev, and test
We can create separate configuration files by creating configuration files or ‘yml’ property files.
- 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.

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
server.port=9000
Let’s make some changes in application-dev.yml and application-test.yml to change port details for both dev and test environment.
# application-dev.yml server: port: 8090
# application-test.yml server: port: 7070
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
2019-07-22 23:47:42.444 INFO 14324 - – [ main] c.c.p.SpringbootProfilesDemoApplication : The following profiles are active: dev 2019-07-22 23:47:44.355 INFO 14324 - – [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8090 (http)
When ‘test’ profile was active
2019-07-22 23:49:06.967 INFO 18604 - – [ main] c.c.p.SpringbootProfilesDemoApplication : The following profiles are active: test 2019-07-22 23:49:08.756 INFO 18604 - – [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 7070 (http)
Logs when the ‘dev’ profile was active
2019-07-22 23:50:05.854 INFO 17820 - – [ main] c.c.p.SpringbootProfilesDemoApplication : No active profile set, falling back to default profiles: default 2019-07-22 23:50:07.692 INFO 17820 - – [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9000 (http)
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
package com.codedelay.profiles; import javax.annotation.PostConstruct; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Profile("test") @Configuration public class TestConfigProfile { @PostConstruct public void onLoadTestEnvironment() { System.out.println("Test environment loaded"); } }
The spring container will process the above class only if the ‘test’ profile is active.
Scenario -1 When the ‘dev’ profile is active.
2019-07-22 23:42:08.847 INFO 17128 - – [ main] c.c.p.SpringbootProfilesDemoApplication : The following profiles are active: dev
Scenario – 2 When the ‘test’ profile is active.
2019-07-22 23:43:31.029 INFO 13372 - – [ main] c.c.p.SpringbootProfilesDemoApplication : The following profiles are active: test 2019-07-22 23:43:33.097 INFO 13372 - – [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1999 ms Test environment loaded
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.

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.

2019-07-23 06:36:02.333 INFO 20376 - – [ main] c.c.p.SpringbootProfilesDemoApplication : The following profiles are active: dev 2019-07-23 06:36:04.376 INFO 20376 - – [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8090 (http)
Conclusion
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.