如何基于属性文件中的属性值实例化@configuration bean

问题描述 投票:0回答:2

我有两个使用@configuration注释的类/ bean,我必须根据属性文件中的值进行实例化。

RemoteServer1.java @组态 公共类RemoteServer1 { //一些认证逻辑在这里 }

RemoteServer2.java @组态 公共类RemoteServer2 { //一些认证逻辑在这里 }

application.properties remote.server.location = RemoteServer1

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

spring spring-boot
2个回答
1
投票

我建议你研究一下Spring Boot中的@Contidional...注释,以有条件地激活Beans,Configurations等。

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)

搜索酒店namehavingValue上的匹配,如果该酒店缺少,将不会评价为true


0
投票

如果要引用属性文件,请使用“$ {}”语法。例如,

@Value("${some.prop}")
private String remoteServer

它将拉取值并将其自动配置为String

© www.soinside.com 2019 - 2024. All rights reserved.