Setting the default active profile in Spring-boot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a widely-used framework that simplifies the configuration and deployment of applications. One of its powerful features is the ability to maintain multiple profiles, which allow applications to separate different configurations for different environments such as development, testing, and production. In this article, we will explore how to set the default active profile in a Spring Boot application.
Understanding Spring Profiles
Spring profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Profiles allow you to configure certain beans for specific environments, enabling a clean separation of concerns.
Spring Boot uses properties files typically named application-{profile}.properties and YAML files application-{profile}.yaml for managing these configurations. For example, you might have:
application-dev.propertiesfor development.application-prod.propertiesfor production.
Default Profile
By default, when no profile is activated, Spring Boot uses the default profile. You can specify properties with application.properties or application-default.properties.
Setting the Default Active Profile
You might want to specify a default active profile so that your application runs with this profile when no other profiles are specified. There are several ways to accomplish this task:
1. Using application.properties
Add the following line to your application.properties:
This means, unless overridden, the dev profile will be active.
2. System Property
You can also activate a profile by setting a system property when launching your application:
3. Programmatically in SpringApplication
You can set the default profiles programmatically in your main method:
4. Using application-test.properties for Tests
Spring Boot will automatically load application-test.properties when running tests. This allows you to have test-specific configurations without any additional profile settings.
Profile-Specific Configuration
Profiles can house specific configurations that depend on the environment. For example:
Profile Activation Order
Profiles can be managed and ordered, with overriding abilities depending on how they are set:
| Setting Method | Overridden By | Order (Lowest to Highest Priority) |
setDefaultProfiles() | application.properties | 1 |
application.properties | System Property | 2 |
| Command Line Argument | N/A | 3 |
Combining Multiple Profiles
Spring Boot allows activating more than one profile at the same time. This can be done by specifying a comma-separated list:
Profile-Specific Files
One way to extend configurations further is by combining profile-specific files. For example, if both dev and qa profiles are active, properties in both application-dev.properties and application-qa.properties will be considered.
Conclusion
Spring Boot profiles provide a flexible approach to managing your application's configuration across different environments. Setting a default active profile is straightforward and ensures that your applications run under a specific environment configuration whenever needed. By understanding and utilizing these configurations wisely, developers can streamline their workflow and hasten their deployment processes, ensuring each environment is appropriately managed.
When using profiles, always ensure that sensitive data (such as passwords) is managed securely, and leverage tools like Spring Cloud Config or Vault for added security and versioning.
Related reading
- Setting the default Java character encoding
- Shaded/Repackaged jar as a dependency
- Sharing src/test classes between modules in a multi-module maven project
- Short form for Java if statement
- Shortest way of checking if Double is NaN
- Should a static final Logger be declared in UPPER-CASE?
- Should I always use a parallel stream when possible?
- Should I avoid the use of set(Preferred|Maximum|Minimum) size methods in Java Swing?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.