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

时间:2017-08-08 19:39:44

标签: spring spring-boot

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

RemoteServer1.java

@Configuration
公共类RemoteServer1 {
//这里有一些认证逻辑 }



RemoteServer2.java

@Configuration
公共类RemoteServer2 {
//这里有一些认证逻辑 }


application.properties
 remote.server.location = RemoteServer1

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

2 个答案:

答案 0 :(得分:1)

我建议您查看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)

查找属性namehavingValue上的匹配,如果遗失该属性,则不会评估为true

答案 1 :(得分:0)

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

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

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

相关问题