【问题标题】:How can i exclude spring bean from autowiring candidates for multiple profiles?如何从多个配置文件的自动装配候选者中排除 spring bean?
【发布时间】:2016-12-17 05:14:28
【问题描述】:

如果当前配置文件不是数组之一,我想从自动装配候选者中排除 bean,如下所示:

@Component
@ActiveProfiles(value = {"!profile_1", "!profile_2"})
public class MyComponentImpl implements MyComponent

@Component
public class MyComponentDummy implements MyComponent

但是当我尝试通过 @ActiveProfiles 注释来执行此操作时,似乎没有排除 bean 并且我得到了 expected single matching bean but found 2。

我是否遗漏了什么,或者那样做是不可能的,必须用类似的东西来完成

@ActiveProfiles(value = {"profile_3", "!profile_4", ... }) 

我的spring版本是4.2.2

【问题讨论】:

    标签: java spring


    【解决方案1】:

    这可能类似于在此处查询How to conditionally declare Bean when multiple profiles are not active?

    另一方面,我建议进行正选择而不是负消除。 “默认”配置文件在这里可能会有所帮助。 假设您有多个配置文件 - Profile1.. Profile5

    如果是 Profile1 或 Profile2,您不想创建 bean 的实例

    所以你可以用 @ActiveProfiles(value={"Profile3","Profile4","Profile5","default")

    【讨论】:

    • 是的,你的链接上的问题完全一样,对不起
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 2018-06-15
    • 2015-08-06
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多