Skip to main content

Spring profiles

Updated 1 min readMarkdown

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: https://github.com/spring-projects/spring-framework/issues/17063

We can use & to define

See also: Profiles JavaDoc


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

Reference

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles

No comments yet. Be the first to comment.

More from this blog