首页 > 解决方案 > 如何在 Kotlin 中排除多个 Spring 配置文件?

问题描述

我使用了这个结构:

@Profile("!test")

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

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

并得到这个例外:

引起:org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有“ru.example.service.AuthenticationService”类型的合格bean可用:预期单个匹配bean但找到2:testAuthenticationService,springAuthenticationService

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

标签: springkotlinspring-profiles

解决方案


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

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

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


推荐阅读