Spring Boot
default profile
active profile
application properties
configuration

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.

Browse interview questions

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.properties for development.
  • application-prod.properties for 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:

properties
spring.profiles.active=dev

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:

bash
java -jar -Dspring.profiles.active=dev your-app.jar

3. Programmatically in SpringApplication

You can set the default profiles programmatically in your main method:

java
1public static void main(String[] args) {
2    SpringApplication app = new SpringApplication(MySpringBootApplication.class);
3    app.setDefaultProfiles("dev");
4    app.run(args);
5}

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:

yaml
1# application-dev.yaml
2server:
3  port: 8080
4spring:
5  datasource:
6    url: jdbc:h2:mem:devdb
7    username: sa
8    password: password
yaml
1# application-prod.yaml
2server:
3  port: 80
4spring:
5  datasource:
6    url: jdbc:mysql://prod-db-server/mydb
7    username: prod_user
8    password: prod_password

Profile Activation Order

Profiles can be managed and ordered, with overriding abilities depending on how they are set:

Setting MethodOverridden ByOrder (Lowest to Highest Priority)
setDefaultProfiles()application.properties1
application.propertiesSystem Property2
Command Line ArgumentN/A3

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:

properties
spring.profiles.active=dev,qa

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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.