Mastering Spring Boot profiles in application.properties

Dive into Spring Boot profiles — your key to tailoring configurations with precision in application.properties.

Patryk Sosinski
5 min readDec 22, 2023

Introduction

In the realm of Spring Boot development, where versatility and adaptability reign supreme, the management of application configurations across various environments becomes a pivotal concern. Introducing the game-changer: the utilization of profiles within Spring Boot’s application.properties.

What are Spring Boot Profiles?

Spring Boot profiles allow you to define sets of configurations that can be activated based on different environments or conditions. They help in managing application settings such as database configurations, logging levels, or other properties tailored specifically for development, testing, staging, or production environments.

Profiles provide a way to segregate and fine-tune configurations, ensuring your application behaves optimally in various scenarios without requiring code changes.

By activating different profiles, you can control how your Spring Boot application behaves, making it versatile and adaptable to different deployment environments or use cases.

Defining application.properties with profiles

To use properties defined in application.properties we need to create such files. The most common location is src/main/resources.

We can also use the .yaml extension for the properties file - Spring recognizes both of the extensions.

STEP 1: Create separate property files for each profile

Create separate property files named application-{profile}.properties for each profile you want to define. For example:

  • application.properties for common configuration
  • application-dev.properties for development
  • application-prod.properties for production

STEP 2: Define configurations

Use the main application.properties file to specify the common settings.

application.properties

In each profile-specific property file, specify configurations relevant to that particular environment. For instance, you might set different database URLs, logging levels, or other environment-specific properties:

application-dev.properties
application-prod.properties

At this point, you may be wondering, what happens when the same property is specified in the profile-specific (application-prod.properties) configuration and in the common (application.properties) configuration.

In this case, the most specific configuration will always win, so in our scenario — the value from application-prod.properties.

Get the properties defined in application.properties

There are a few ways to get the properties in the application. The important thing to keep in mind is that the class responsible for reading the property from the configuration file has to be a Spring bean.

Spring’s @Value annotation

@Value annotation usage

Environment

Environment usage

@ConfigurationProperties class

This approach is a bit different. We define a standard Java class that holds all of the needed properties. Then we can inject this class in different places and access the needed properties.

@ConfigurationProperties usage

Activating Profiles

There are a few ways to activate a profile we would like to load properties for.

Using spring.profiles.active in main application.properties

As I mentioned before, we can create a properties file that stores all of the common configurations for the profiles. In this file, we can set a profile that will be activated after application startup.

application.properties with an active profile

Command-line argument

The property mentioned in the previous point, we can set when running the application from the command line.

java -jar -Dspring.profiles.active=dev {YOUR-APPLICATION}.jar

#Example
java -jar -Dspring.profiles.active=dev demo-app.jar

Environment variable

We can set an environment variable and the profile will be activated on all the applications that have been launched in this environment.

Windows:

set SPRING_PROFILES_ACTIVE=dev

MacOS:

export SPRING_PROFILES_ACTIVE=dev

IntelliJ config

We can also set the profile in Intelli’s run configuration.

IntelliJ — edit run configuration
IntelliJ — add a profile in the run configuration

NOTE: It may be that you don’t see the Active profiles option. You can enable it using the Modify options:

enable Active profiles

Use cases

How application.properties used with Spring Boot profiles can help you in everyday programming? What are the most common use cases?

Development, Testing, Production Environments

Tailor database settings, logging levels, API endpoints, etc., for different environments.

Feature Flags or Toggle Configurations

Enable or disable certain features based on profiles, allowing controlled feature rollouts.

Third-Party Service Configurations

Configure API endpoints, credentials, or timeouts for different services used in various environments.

Test-Specific Configurations

Customize configurations for testing suites, like using a separate test database or mocking certain services.

Blue/Green or Canary Deployments

Adjust configurations to control how the application behaves in different deployment strategies.

Client-specific Configurations

Customize configurations for different clients or regions, such as localization settings or API variations.

Resource Allocation

Adjust thread pool sizes, caching strategies, or connection pool settings for different environments.

Scaling Configurations

Set up different configurations for load balancers, scaling thresholds, or timeouts in various deployment scenarios.

Containerization or Cloud Configurations

Customize settings for microservices, containerized deployments, or cloud-specific configurations.

Conclusion

In the dynamic landscape of software development, the ability to seamlessly adapt configurations to diverse environments is paramount. Spring Boot’s application.properties with profiles emerges as a powerful tool, offering a versatile mechanism to manage configurations across various deployment stages, scenarios, and specific use cases.

In essence, application profiles in application.properties empower developers to create adaptable, robust, and easily maintainable applications. With the ability to manage configurations seamlessly across various environments and deployment strategies, Spring Boot profiles pave the way for consistency, scalability, and enhanced security without the complexities of extensive code modifications.

Thank you for reading this post, I hope you learned something new and useful.

If you found this content helpful, a 👏 would mean a lot! Feel free to drop a comment for any queries or suggestions. Do you have a topic in mind for the next post? Share your ideas too!

--

--