Spring Boot Profiles

Profiles let you run the same app with different configurations depending on the environment. Your app uses one database on your laptop, a different one on the test server, and a third in production — all from the same codebase.

The Three-Stage Launch Pad Analogy

Development (dev)    →   Test (test)   →   Production (prod)
───────────────────      ────────────      ─────────────────
Local H2 database        MySQL staging     MySQL production
Debug logging on         Info logging      Warn logging only
Detailed errors          Basic errors      Hidden errors
Port 8080                Port 8081         Port 80

Profiles let you switch between these stages without editing a single line of Java code.

Creating Profile-Specific Property Files

Create one file per environment, following the naming pattern application-{profile}.properties:

src/main/resources/
├── application.properties          ← Shared settings (all environments)
├── application-dev.properties      ← Dev-only settings
├── application-test.properties     ← Test-only settings
└── application-prod.properties     ← Production-only settings

application.properties (shared)

spring.application.name=estudy247-app
server.port=8080

application-dev.properties

spring.datasource.url=jdbc:h2:mem:devdb
spring.jpa.hibernate.ddl-auto=create-drop
logging.level.com.estudy247=DEBUG

application-prod.properties

spring.datasource.url=jdbc:mysql://prod-server:3306/maindb
spring.datasource.username=produser
spring.datasource.password=${DB_PASS}
spring.jpa.hibernate.ddl-auto=validate
logging.level.root=WARN

How Profiles Merge

                 application.properties
                         │
                         ▼ base settings loaded
              application-dev.properties
                         │
                         ▼ dev settings overlay (overrides base)
                 Final configuration

Profile-specific files supplement the base file. Settings in the profile file override matching keys in the base file. Settings that exist only in the base file remain active.

Activating a Profile

Option 1 — In application.properties

spring.profiles.active=dev

Option 2 — Command-line argument

java -jar app.jar --spring.profiles.active=prod

Option 3 — Environment variable

export SPRING_PROFILES_ACTIVE=prod

The command-line and environment variable methods are used in production deployments where you cannot modify the JAR file.

Profile-Specific Beans

Use @Profile to activate a bean only in a specific environment:

@Component
@Profile("dev")
public class FakePaymentGateway implements PaymentGateway {
    // Returns mock success — safe for local testing
    public boolean charge(double amount) { return true; }
}

@Component
@Profile("prod")
public class StripePaymentGateway implements PaymentGateway {
    // Calls the real Stripe API
    public boolean charge(double amount) { ... }
}
Environment    Bean Used
───────────    ──────────────────────
dev            FakePaymentGateway
prod           StripePaymentGateway

This pattern lets you test payment flows locally without charging real cards.

Checking the Active Profile in Code

@Component
public class StartupLogger {

    @Autowired
    private Environment env;

    @PostConstruct
    public void log() {
        System.out.println("Active profiles: " +
            Arrays.toString(env.getActiveProfiles()));
    }
}

Multiple Active Profiles

Activate more than one profile at once by separating them with commas:

spring.profiles.active=dev,mock-email

This activates both the dev config and a separate mock-email profile that disables real email sending during development.

Summary

  • Profiles let one codebase run differently across dev, test, and production
  • Name profile files as application-{profile}.properties
  • Profile files merge with the base file — profile settings win on conflicts
  • Activate profiles via properties, command-line args, or environment variables
  • @Profile on a bean registers it only when that profile is active

Leave a Comment

Your email address will not be published. Required fields are marked *