Spring Profile注释允许您选择配置文件.但是,如果您阅读文档,它只允许您使用OR操作选择多个配置文件.如果指定@Profile("A","B"),那么如果配置文件A或配置文件B处于活动状态,则bean将启动.
我们的用例不同,我们希望支持多种配置的TEST和PROD版本.因此,有时我们只想在配置文件TEST和CONFIG1都处于活动状态时自动装配bean.
有没有办法用Spring做到这一点?什么是最简单的方法?
在我的Spring-Boot-App中,我想有条件地声明一个Bean,这取决于(un)加载的spring-profiles.
条件:
Profile "a" NOT loaded
AND
Profile "b" NOT loaded
Run Code Online (Sandbox Code Playgroud)
到目前为止我的解决方案(有效):
@Bean
@ConditionalOnExpression("#{!environment.getProperty('spring.profiles.active').contains('a') && !environment.getProperty('spring.profiles.active').contains('b')}")
public MyBean myBean(){/*...*/}
Run Code Online (Sandbox Code Playgroud)
是否有更优雅(和更短)的方式来解释这种情况?
特别是我想在这里摆脱Spring Expression Language的使用.