Profiles in Spring Boot: Managing Multiple Environments

When developing an application, you often need different configurations for different environments. For example:

  • In development, you might want verbose logging and a local database.
  • In testing, you might use a mock service or in-memory database.
  • In production, you need optimized settings, security, and real services.

Managing these differences manually can be messy. This is where Profiles in Spring Boot come to the rescue.

In this guide, we’ll walk you through:

  • ✅ What are Spring Profiles?
  • ✅ How to create and structure environment-specific configurations
  • ✅ How to activate and switch between profiles
  • ✅ How to load profile-specific beans
  • ✅ Best practices and common pitfalls
Profiles in Spring Boot

✅ What Are Spring Profiles?

Spring Profiles allow developers to create logical groupings of beans and configurations which can be registered and activated conditionally. This means you can isolate your configurations and activate only the ones relevant for a specific environment.

For example:

  • Load application-dev.properties for development
  • Load application-prod.properties for production

With profiles, Spring Boot lets you:

  • Control which configuration files are loaded
  • Customize bean definitions per environment
  • Simplify deployment and reduce configuration errors

📁 Creating Environment-Specific Property Files

Spring Boot, by default, looks for a file named application.properties or application.yml. To use profiles, you create profile-specific files.

🔸 Using .properties

Each of these files can contain different property values depending on the environment.

Example:

application-dev.properties

application-prod.properties

🔸 Using application.yml with Profiles

🚀 Activating a Profile

Spring Boot provides multiple ways to activate a profile.

✅ 1. In application.properties or application.yml

✅ 2. Via Command Line

✅ 3. As an Environment Variable (e.g., in Docker or CI)

✅ 4. JVM System Property (useful in IDEs)

💡 Tip: Never hardcode production profiles inside the code. Use command-line or environment variables to control it externally.

🧠 Using @Profile to Load Beans Conditionally

Spring allows you to load specific beans only for a particular profile.

Example:

With this setup:

  • If the active profile is dev, you get an H2 in-memory database.
  • If the active profile is prod, you connect to a production PostgreSQL database.

🧪 Sample Controller to Demonstrate Profiles

1. Add a Message in application-dev.properties

2. Add a Message in application-prod.properties

3. Use in a Controller

Output

  • If spring.profiles.active=dev, response will be:
    “Running in DEV mode!”
  • If spring.profiles.active=prod, response will be:
    “Running in PROD mode!”

⚠️ Common Pitfalls

🔸 Not setting any profile: If no active profile is defined, only application.properties is used. Spring won’t load any application-<profile>.properties files unless a profile is active.

🔸 Conflicting properties: If the same key exists in application.properties and a profile-specific file, the profile-specific value will override the default.

🔸 Forgetting to use @Profile: Even if the profile is active, beans without @Profile annotations will always be created.

✅ Best Practices

✅ Use profile-specific files to isolate configurations.
✅ Keep sensitive information like passwords out of source control—use environment variables or external config services.
✅ Set a default profile or provide fallback behavior.
✅ Document which profiles are supported and when to use them.

📌 Summary

FeatureBenefit
application-<profile>.propertiesCustomizes properties per environment
spring.profiles.activeLets you switch environments dynamically
@ProfileControls bean loading based on active profile
Multiple activation optionsFlexible for local, CI/CD, cloud

🎯 Final Thoughts

Spring Boot profiles provide an elegant and flexible way to manage configuration across different stages of application development. With just a bit of setup, you can easily avoid config-related errors and ensure your app behaves as expected in each environment.

“Environment-specific configuration is no longer a hassle with Spring Profiles.”

📘 External References