[英]Spring configuration based on multiple active profiles
我的應用程序包含Linux和Windows等2個不同目標環境的划分,以及DTAP的划分。 是否可以基於多個活動配置文件加載不同的屬性文件?
我們當前的設置包含一個使用ant處理的文件夾結構:
視窗:
Linux的:
目標是擁有{OS-active}-{environment-active} -application.properties之類的內容。 為8個活動環境之一加載正確的屬性,但還要基於OS激活基本屬性。
有沒有一種方法可以使用Spring配置文件直接使用Spring來做到這一點?
我將執行屬性:
@Bean
@Primary
fun properties(context: AbstractApplicationContext?): PropertySourcesPlaceholderConfigurer {
val propertySourcesPlaceholderConfigurer = PropertySourcesPlaceholderConfigurer()
val yaml = YamlPropertiesFactoryBean()
val os = System.getProperty("os.name").replace(" ", "")
val resources = context?.environment?.activeProfiles
?.map { profile ->
ClassPathResource("$os-$profile-application.yml")
}?.plus(ClassPathResource("$os-base-application.yml"))
?.toTypedArray()?: emptyArray()
yaml.setResources(*resources)
propertySourcesPlaceholderConfigurer.setProperties(yaml.`object`!!)
return propertySourcesPlaceholderConfigurer
}
例如,在MacOS上,您最終會得到:
MacOSX-test-application.yml
MacOSX-base-application.yml
它在科特林,但我想您有主意!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.