【问题标题】:How to exclude multiple Spring profiles in Kotlin?如何在 Kotlin 中排除多个 Spring 配置文件?
【发布时间】:2021-08-30 14:01:42
【问题描述】:

我使用了这个结构:

@Profile("!test")

好的,但我需要设置不要将此 bean 与多个配置文件一起使用。只要 value 字段是 String[],我是这样写的:

@Profile(value = ["!local", "!test"])

并得到这个异常:

原因: org.springframework.beans.factory.NoUniqueBeanDefinitionException: 否 'ru.example.service.AuthenticationService' 类型的限定 bean 可用:预期单个匹配 bean,但找到 2: testAuthenticationService,springAuthenticationService

如我们所见,上面的构造不起作用。在我的情况下如何设置个人资料?

【问题讨论】:

  • 您是否将 @Profile("test") 放在 testAuthenticationService bean 或 Autowired ctor 上?
  • 投入使用。
  • 我想我知道问题出在哪里了,你可以试试@Profile("!test & !local")
  • 我的理论是您有 2 个配置文件“!local”(包括测试)和“!test”(包括本地),因此您需要有一个排除这两个配置文件的单个配置文件
  • 哇!有用!请在下面对我的问题做出正确的回答,我会接受。

标签: spring kotlin spring-profiles


【解决方案1】:

@Profile(value = ["!local", "!test"]) 等同于 `@Profile("!local | !test") 意味着只排除本地和测试的 bean。

您需要的是 `@Profile("!local & !test") 来排除本地或测试。

这是德摩根定律的一个例子,其中!(foo | bar) == !foo & !bar,但你有!foo | !bar == !(foo & bar)。

【讨论】:

    【解决方案2】:

    它有效,但它告诉您,通过拥有这两个活动配置文件,您必须拥有 AuthenticationService 类型的 bean,而只需要一个。 这是您需要解决的问题。 @Profile 与多个配置文件工作得很好。

    【讨论】:

    • 老兄,当我使用 @Profile("!test") 时,我没有任何问题。问题不在于我有 2 个豆子。问题是@Profile 不适用于数组。
    猜你喜欢
    • 2016-12-17
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 2014-11-30
    相关资源
    最近更新 更多