DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

What and How to Configure Spring Profiles?

1. What are Spring Profiles?

1.1 Understanding Spring Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. This feature allows you to define separate configurations for different stages of your application lifecycle.

For example, you might have one set of properties for development, another for testing, and a third for production. By segregating configurations, Spring Profiles make it easy to manage and switch between environments without altering the codebase.

1.2 The Importance of Spring Profiles

In any real-world application, you will have multiple environments. Each environment may require different configurations, such as different databases, APIs, or even different logging levels. Spring Profiles help in managing these configurations seamlessly, ensuring that the application behaves as expected in each environment.

1.3 Default Profile in Spring Boot

Spring Boot uses a default profile called default if no other profile is specified. This means that if you don't explicitly activate a profile, the application will fall back to the default configuration. This can be handy for local development or when a specific profile isn't required.

1.4 Profile-Specific Configuration Files

Spring allows you to create profile-specific configuration files. For example, you can create an application-dev.properties file for the development environment and an application-prod.properties file for production. Spring Boot will automatically pick the correct file based on the active profile.

# application-dev.properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost/devdb

# application-prod.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost/proddb
Enter fullscreen mode Exit fullscreen mode

2. How to Configure Spring Profiles?

2.1 Setting Up Profiles in application.properties

You can specify the active profile directly in the application.properties file by using the spring.profiles.active property.

# application.properties
spring.profiles.active=dev
Enter fullscreen mode Exit fullscreen mode

This approach is straightforward but hardcodes the profile, which may not be ideal for all situations.

2.2 Using @profile Annotation

Another way to configure Spring Profiles is by using the @profile annotation. This annotation can be applied to beans or configuration classes, ensuring that they are only loaded when a specific profile is active.

@Configuration
@Profile("dev")
public class DevConfig {

    @Bean
    public DataSource dataSource() {
        return new DataSource("jdbc:mysql://localhost/devdb");
    }
}
Enter fullscreen mode Exit fullscreen mode

2.3 Activating Profiles via Command Line

Profiles can also be activated via the command line when starting your Spring Boot application. This is particularly useful for CI/CD pipelines where different environments need to be managed.

java -jar myapp.jar --spring.profiles.active=prod
Enter fullscreen mode Exit fullscreen mode

This command activates the prod profile, ensuring that the production-specific configurations are used.

2.4 Demo: Switching Profiles in a Real-World Application

Let’s see a practical demo of switching between profiles. We'll create two profiles, dev and prod , with different logging levels and database configurations.

# application-dev.properties
logging.level.root=DEBUG
spring.datasource.url=jdbc:h2:mem:devdb

# application-prod.properties
logging.level.root=ERROR
spring.datasource.url=jdbc:mysql://localhost/proddb
Enter fullscreen mode Exit fullscreen mode

When we run the application with dev profile:

java -jar myapp.jar --spring.profiles.active=dev
Enter fullscreen mode Exit fullscreen mode

The output should show detailed logs due to the DEBUG level, and the application should connect to the H2 in-memory database.

Running the application with prod profile:

java -jar myapp.jar --spring.profiles.active=prod
Enter fullscreen mode Exit fullscreen mode

The output will show only error logs, and the application will connect to the MySQL production database.

3. Advantages and Best Practices

3.1 Why Use Spring Profiles?

Spring Profiles streamline the process of managing configurations across different environments. They allow for cleaner, more maintainable code by separating concerns, and they help reduce the risk of environment-specific issues.

3.2 Best Practices for Profile Management

  • Use Descriptive Names : Choose profile names that clearly indicate the environment, like dev , test , prod.
  • Avoid Hardcoding Profiles : Rely on external configuration (like command-line arguments) to set the active profile.
  • Document Profiles : Ensure that each profile's purpose and configuration are well-documented for team members.

4. Conclusion

Spring Profiles are an essential feature for any Spring Boot application that needs to run in multiple environments. By understanding and effectively managing profiles, you can ensure your application behaves consistently across development, testing, and production stages.

Have any questions about configuring Spring Profiles or how to implement them in your project? Feel free to leave a comment below! I'm here to help.

Read posts more at : What and How to Configure Spring Profiles?

Top comments (0)