Spring profiles

·

1 min read

Config

In application.properties

spring.profiles.active=prod

Activate multiple profiles:

spring.profiles.active=local,test

@Profile Annotation Make a component/configuration class / bean available in certain profiles.

@Profile annotation Javadoc

@Configuration
class FooConfig {
    @Bean
    @Profile("prod")
    public Foo foo() {
        return new Foo();
    }
}

Multiple profiles:

@Configuration
class FooConfig {
    @Bean
    @Profile({"prod", "stg"})
    public Foo foo() {
        return new Foo();
    }
}

NOT operator

Configure a bean when the specified profiles are not active. In this example, the bean will be enabled if local is not enabled OR prod is enabled

@Configuration
class FooConfig {
    @Bean
    @Profile({"!local", "prod"})
    public Foo foo() {
        return new Foo();
    }
}

AND operator

Caveat: If having multiple NOT operator, the behavior is still OR.

Issue: github.com/spring-projects/spring-framework..

We can use & to define

See also: Profiles JavaDoc


class FooConfig {
    @Bean
    @Profile("!local" & !prod")
    public Foo foo() {
        return new Foo();
    }
}

Reference

docs.spring.io/spring-boot/docs/current/ref..