gpt4 book ai didi

spring - 如何根据属性文件中的属性值实例化@configuration bean

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

我有两个用 @Configuration 注释的类/bean,我必须实例化基于属性文件中的值。

RemoteServer1.java

@Configuration
public class RemoteServer1 {
//some authentication logic goes here
}

RemoteServer2.java

@Configuration
public class RemoteServer2 {
//some authentication logic goes here
}

应用程序属性

remote.server.location=RemoteServer1

现在我想实例化与属性文件中的值匹配的@Configuration类/bean。

最佳答案

我建议您查看 Spring Boot 中的 @Contidional... 注释,以有条件地激活 Bean、配置等。

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.html

应该在查看属性和配置时起作用。对于第一个配置,

@ConditionalOnProperty(name="remote.server.location", havingValue="RemoteServer1",  matchIfMissing=false)

其次,

@ConditionalOnProperty(name="remote.server.location", havingValue="RemoteServer2",  matchIfMissing=false)

查找属性 name,匹配 havingValue,如果缺少该属性,则不会计算为 true

关于spring - 如何根据属性文件中的属性值实例化@configuration bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45576776/

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