gpt4 book ai didi

java - @ConditionalOnProperty 默认 bean 当前不工作

转载 作者:行者123 更新时间:2023-12-02 17:04:04 25 4
gpt4 key购买 nike

我有 2 个 bean :

@Component("CryptoClient")
@ConditionalOnProperty(name = "enabled-client", havingValue = "AfsClient")
public class AfsClient implements CryptoClient {

@Component("CryptoClient")
@ConditionalOnProperty(name = "enabled-client", havingValue = "JinnClient")
public class JinnClient implements CryptoClient {

我更改属性值:

enabled-client: AfsClientenabled-client: JinnClient 并使用这个 bean。但是现在我添加了默认 bean(如果 enabled-client 属性不存在):

    @Bean("CryptoClient")
@ConditionalOnProperty(name = "enabled-client", matchIfMissing = true)
public CryptoClient defaultClient(EDSService edsService) {
return new AfsClient(edsService);
}

如果 enabled-client 属性不存在 - 它工作正常。但即使存在属性(property),它也能起作用。无论如何调用默认bean。

最佳答案

先了解一下ma​​tchIfMissing属性:用于指定配置文件中没有配置相应属性时的默认处理。

默认情况下,ma​​tchIfMissing为false,即如果不配置该属性,则自动配置不生效,不创建bean。如果ma​​tchIfMissing为true,表示如果没有对应的属性配置,自动配置默认生效,创建bean(本例为defaultClient),如果对应存在属性配置 - 显然它将创建 bean。 :)

因此,如果 ma​​tchIfMissing 设置为 true - 在任何情况下都会创建 bean defaultClient

关于java - @ConditionalOnProperty 默认 bean 当前不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52624012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com