我有两个使用@configuration注释的类/ bean,我必须根据属性文件中的值进行实例化。
RemoteServer1.java @组态 公共类RemoteServer1 { //一些认证逻辑在这里 }
RemoteServer2.java @组态 公共类RemoteServer2 { //一些认证逻辑在这里 }
和
application.properties remote.server.location = RemoteServer1
现在我想实例化@Configuration类/ bean与属性文件中的值匹配。
我建议你研究一下Spring Boot中的@Contidional...
注释,以有条件地激活Beans,Configurations等。
这应该看看属性和配置。对于第一个配置,
@ConditionalOnProperty(name="remote.server.location", havingValue="RemoteServer1", matchIfMissing=false)
第二,
@ConditionalOnProperty(name="remote.server.location", havingValue="RemoteServer2", matchIfMissing=false)
搜索酒店name
,havingValue
上的匹配,如果该酒店缺少,将不会评价为true
。
如果要引用属性文件,请使用“$ {}”语法。例如,
@Value("${some.prop}")
private String remoteServer
它将拉取值并将其自动配置为String