0
0
SpringbootHow-ToBeginner · 3 min read

How to Use Profiles in Spring Boot for Environment Configuration

In Spring Boot, use profiles to separate configuration for different environments like development or production. Activate a profile by setting spring.profiles.active in application.properties or via command line, and create profile-specific files like application-dev.properties for environment-specific settings.
📐

Syntax

Spring Boot profiles let you define environment-specific settings. Use spring.profiles.active to activate a profile. Create files named application-{profile}.properties or application-{profile}.yml for each profile.

Example parts:

  • spring.profiles.active=dev activates the dev profile.
  • application-dev.properties holds config for the dev profile.
  • Beans can be annotated with @Profile("dev") to load only in that profile.
properties
spring.profiles.active=dev

# application-dev.properties
server.port=8081

# application-prod.properties
server.port=80

// Java bean example
@Configuration
@Profile("dev")
public class DevConfig {
    // beans for dev
}
💻

Example

This example shows how to define two profiles: dev and prod. Each profile has its own port setting. The active profile is set in application.properties. The app will run on port 8081 when dev is active and port 80 when prod is active.

properties/java
# application.properties
spring.profiles.active=dev

# application-dev.properties
server.port=8081

# application-prod.properties
server.port=80

// Sample Spring Boot main class
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
Output
Application started on port 8081
⚠️

Common Pitfalls

Common mistakes include:

  • Not setting spring.profiles.active, so the default profile runs.
  • Forgetting to create profile-specific config files, causing missing settings.
  • Using @Profile annotation incorrectly or on beans that should always load.
  • Activating multiple profiles without understanding precedence.
properties
/* Wrong: No active profile set, default config used */
# application.properties
server.port=8080

/* Right: Activate dev profile */
# application.properties
spring.profiles.active=dev
📊

Quick Reference

  • Activate profile: Set spring.profiles.active in application.properties or pass --spring.profiles.active=dev as a command line argument.
  • Profile config files: Use application-{profile}.properties or application-{profile}.yml.
  • Profile beans: Annotate beans with @Profile("profileName") to load conditionally.
  • Multiple profiles: Separate with commas, e.g., dev,debug.

Key Takeaways

Use spring.profiles.active to select which profile runs your app.
Create application-{profile}.properties files for environment-specific settings.
Use @Profile annotation to load beans only for certain profiles.
Always test your app with different profiles to avoid config errors.
Multiple profiles can be active, but understand their order and overrides.