2017-08-08 77 views
0

我有兩個使用@configuration註解的類/ bean,我必須根據屬性文件中的值實例化 。如何根據屬性文件中的屬性值實例化@configuration bean

RemoteServer1.java

@Configuration
公共類RemoteServer1 {
//一些驗證邏輯放在這裏
}



RemoteServer2.java

@配置
公共類RemoteServer2 {
//一些驗證邏輯放在這裏
}


application.properties
remote.server.location = RemoteServer1

現在我想要實例化與屬性文件中的值匹配的@Configuration類/ bean。

回答

0

我建議你看看在春天開機@Contidional...註釋有條件地激活豆類,配置等

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

+0

它的工作非常感謝@Darren福賽斯 – laxmi

0

如果要對屬性文件進行引用,請使用「$ {}」語法。例如,

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

這將拉動值,並將其自動配置到字符串