# Spring profiles

# 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](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html)

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

# Multiple profiles:

```java
@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

```java
@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](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Profiles.html#of-java.lang.String...-)

```

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 


