0
0
SpringbootHow-ToBeginner · 3 min read

How to Activate Profile in Spring Boot: Simple Guide

In Spring Boot, activate a profile by setting spring.profiles.active in application.properties or application.yml, or by passing --spring.profiles.active=profileName as a command line argument. You can also set it as an environment variable SPRING_PROFILES_ACTIVE before running your app.
📐

Syntax

To activate a Spring Boot profile, use the spring.profiles.active property. This can be set in configuration files, command line, or environment variables.

  • In application.properties: spring.profiles.active=profileName
  • In application.yml: spring.profiles.active: profileName
  • Command line: --spring.profiles.active=profileName
  • Environment variable: SPRING_PROFILES_ACTIVE=profileName
properties
spring.profiles.active=dev
💻

Example

This example shows how to activate the dev profile using application.properties and how Spring Boot loads the matching configuration.

java
src/main/resources/application.properties:
spring.profiles.active=dev

src/main/resources/application-dev.properties:
app.message=Hello from Dev Profile

// Java main class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class MessageController {
    @Value("${app.message}")
    private String message;

    @GetMapping("/")
    public String getMessage() {
        return message;
    }
}
Output
Hello from Dev Profile
⚠️

Common Pitfalls

Common mistakes when activating profiles include:

  • Setting spring.profiles.active in multiple places causing conflicts.
  • Misspelling profile names, so Spring Boot cannot find the matching config.
  • Forgetting to create profile-specific config files like application-dev.properties.
  • Using uppercase letters in profile names inconsistently.

Always verify the active profile by checking logs or using Environment bean.

properties
/* Wrong: conflicting profiles in properties and command line */
// application.properties
spring.profiles.active=dev

// Command line
java -jar app.jar --spring.profiles.active=prod

/* Right: set profile in only one place to avoid confusion */
// Prefer command line or environment variable for overrides
java -jar app.jar --spring.profiles.active=prod
📊

Quick Reference

MethodHow to SetUsage
application.propertiesspring.profiles.active=profileNameDefault profile activation
application.ymlspring.profiles.active: profileNameYAML config format
Command line--spring.profiles.active=profileNameOverride at runtime
Environment variableSPRING_PROFILES_ACTIVE=profileNameSet in OS environment

Key Takeaways

Set spring.profiles.active to activate a profile in Spring Boot.
Use application.properties, command line, or environment variables to specify the active profile.
Avoid conflicting profile settings in multiple places.
Profile names are case-sensitive and must match config files.
Check logs or Environment bean to confirm the active profile.