我可以否定 (!) 一组弹簧配置文件吗? [英] Can I negate (!) a collection of spring profiles?
问题描述
是否可以将 bean 配置为不被一组配置文件使用?目前我可以做到这一点(我相信):
Is it possible to configure a bean in such a way that it wont be used by a group of profiles? Currently I can do this (I believe):
@Profile("!dev, !qa, !local")
有没有更简洁的符号来实现这一点?假设我有很多个人资料.另外,如果我有某个服务(或其他)的 Mock 和具体实现,我可以只注释其中一个,并假设另一个将用于所有其他情况吗?换句话说,这是否是必要的:
Is there a neater notation to achieve this? Let's assume I have lots of profiles. Also, if I have a Mock and concrete implementation of some service (or whatever), Can I just annotate one of them, and assume the other will be used in all other cases? In other words, is this, for example, necessary:
@Profile("dev, prof1, prof2")
public class MockImp implements MyInterface {...}
@Profile("!dev, !prof1, !prof2") //assume for argument sake that there are many other profiles
public class RealImp implements MyInterface {...}
我可以只注释其中一个,然后在另一个上粘贴 @Primary
注释吗?
Could I just annotate one of them, and stick a @Primary
annotation on the other instead?
本质上我想要这个:
@Profile("!(dev, prof1, prof2)")
提前致谢!
推荐答案
自 Spring 5.1(包含在 Spring Boot 2.1 中)可以在内部使用 profile 表达式profile 字符串注释(参见 Profile.of(..) 了解详情.
Since Spring 5.1 (incorporated in Spring Boot 2.1) it is possible to use a profile expression inside profile string annotation (see the description in Profile.of(..) for details).
所以要从某些配置文件中排除您的 bean,您可以使用这样的表达式:
So to exclude your bean from certain profiles you can use an expression like this:
@Profile("!dev & !prof1 & !prof2")
也可以使用其他逻辑运算符,例如:
Other logical operators can be used as well, for example:
@Profile("test | local")
这篇关于我可以否定 (!) 一组弹簧配置文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!