Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt

Use this file to discover all available pages before exploring further.

Spring profiles give you a way to segregate parts of your application configuration and make them available only in specific environments. Any @Component, @Configuration, or @ConfigurationProperties class can be marked with @Profile to restrict when it is loaded. Pair profiles with profile-specific property files to manage environment differences — database URLs, feature flags, logging levels — without changing application code.

Marking beans with @Profile

Annotate a component or configuration class with @Profile to make it conditional on one or more active profiles:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("production")
public class ProductionConfiguration {
    // beans only registered when the "production" profile is active
}
You can combine profiles using logical expressions:
@Profile("production & (eu-central | eu-west)")
When @ConfigurationProperties beans are registered through @EnableConfigurationProperties rather than scanning, place the @Profile annotation on the @Configuration class that holds @EnableConfigurationProperties, not on the @ConfigurationProperties class itself.

Activating profiles

Via application.properties or application.yaml

spring:
  profiles:
    active: "dev,hsqldb"

Via the command line

$ java -jar myapp.jar --spring.profiles.active=dev,hsqldb
Command-line arguments take precedence over file-based properties, so you can override the active profile at launch without modifying any files.

Programmatically

Call SpringApplication.setAdditionalProfiles(...) before the application runs. This adds profiles on top of any already configured through properties:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.setAdditionalProfiles("local");
        application.run(args);
    }

}
You can also activate profiles at any time through Spring’s ConfigurableEnvironment interface on an already-running context.

Default profile

When no profile is explicitly active, Spring Boot enables the default profile. Properties defined in application-default.properties (or application-default.yaml) are loaded automatically in this case. You can change the name of the default profile:
spring:
  profiles:
    default: "none"

Profile-specific property files

Spring Boot automatically loads application-{profile}.properties (or .yaml) variants alongside the base application.properties. Profile-specific files always override the non-specific file.
src/main/resources/
  application.properties          # shared defaults
  application-dev.properties      # development overrides
  application-prod.properties     # production overrides
# application-dev.properties
spring.datasource.url=jdbc:h2:mem:devdb
logging.level.root=DEBUG
# application-prod.properties
spring.datasource.url=jdbc:postgresql://prod-host/mydb
logging.level.root=WARN
When multiple profiles are active and both provide the same property, the last profile listed wins:
spring:
  profiles:
    active: "prod,live"
Values in application-live.properties override those in application-prod.properties.

Adding profiles without replacing

Use spring.profiles.include to add profiles on top of the ones already activated, rather than replacing them:
spring:
  profiles:
    include:
      - "common"
      - "local"
This ensures common and local are always active, regardless of which profile is set via --spring.profiles.active.
spring.profiles.active, spring.profiles.include, and spring.profiles.group can only appear in non-profile-specific documents. Setting them inside a profile-specific file or a document activated by spring.config.activate.on-profile is invalid and will be rejected.

Profile groups

When fine-grained profiles become cumbersome to manage, use profile groups to define a logical name for a set of related profiles:
spring:
  profiles:
    group:
      production:
        - "proddb"
        - "prodmq"
Activating production automatically activates proddb and prodmq:
$ java -jar myapp.jar --spring.profiles.active=production
Profile groups are a clean way to model deployment environments (development, staging, production) while keeping individual feature profiles (proddb, prodmq, metrics) independently testable.

Profile validation

By default, profile names may contain letters, numbers, and the characters -, _, ., +, @, and must start and end with a letter or number. If you need more flexible names, disable validation:
spring:
  profiles:
    validate: false

Profile-specific Logback configuration

In logback-spring.xml, use the <springProfile> tag to include or exclude sections based on active profiles:
<springProfile name="staging">
    <!-- configuration enabled when "staging" profile is active -->
</springProfile>

<springProfile name="dev | staging">
    <!-- configuration enabled when "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
    <!-- configuration enabled when "production" profile is NOT active -->
</springProfile>
This tag must be used in logback-spring.xml, not logback.xml, because standard Logback configuration files are loaded before Spring can provide profile information.

Build docs developers (and LLMs) love