如何在 Kotlin 中排除多个 Spring 配置文件?
How to exclude multiple Spring profiles in Kotlin?
我使用了这个结构:
@Profile("!test")
好的,但我需要设置不要将此 bean 与多个配置文件一起使用。只要值字段是 String[],我就这样写:
@Profile(value = ["!local", "!test"])
并得到这个异常:
Caused by:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type 'ru.example.service.AuthenticationService'
available: expected single matching bean but found 2:
testAuthenticationService,springAuthenticationService
如我们所见,上面的构造不起作用。我的情况如何设置配置文件?
它有效,但它告诉您,通过拥有这两个活动配置文件,您必须使用类型为 AuthenticationService
的 bean,而预期只有一个。
这是你需要修复的。 @Profile
多个配置文件工作正常。
@Profile(value = ["!local", "!test"])
等同于 `@Profile("!local | !test") 意味着只排除本地和测试的 bean。
您需要的是 `@Profile("!local & !test") 来排除本地或测试。
这是德摩根定律的一个示例,其中 !(foo | bar) == !foo & !bar
,但您有 !foo | !bar == !(foo & bar)
。
我使用了这个结构:
@Profile("!test")
好的,但我需要设置不要将此 bean 与多个配置文件一起使用。只要值字段是 String[],我就这样写:
@Profile(value = ["!local", "!test"])
并得到这个异常:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'ru.example.service.AuthenticationService' available: expected single matching bean but found 2: testAuthenticationService,springAuthenticationService
如我们所见,上面的构造不起作用。我的情况如何设置配置文件?
它有效,但它告诉您,通过拥有这两个活动配置文件,您必须使用类型为 AuthenticationService
的 bean,而预期只有一个。
这是你需要修复的。 @Profile
多个配置文件工作正常。
@Profile(value = ["!local", "!test"])
等同于 `@Profile("!local | !test") 意味着只排除本地和测试的 bean。
您需要的是 `@Profile("!local & !test") 来排除本地或测试。
这是德摩根定律的一个示例,其中 !(foo | bar) == !foo & !bar
,但您有 !foo | !bar == !(foo & bar)
。