如何根據需要從我的主屬性檔案中指向不同的屬性檔案?
我的主檔案包含不同的 web 服務型別,以及在哪里可以找到每個服務的特定檔案,在那里我將能夠訪問有關該特定服務的更多資訊。
我有一個主要的屬性檔案
webservices.properties:
webs1=C:\Users\yyy\webs1.properties webs2=C:\Users\yyy\webs2.properties webs3=C:\Users\yyy\webs3.properties webs4=C:\Users\yyy\webs4.properties
在 Web 服務屬性檔案中
webs1.properties:
key1=value1 key2=value2 key3=value3
目前我的應用程式只使用一個 Web 服務。我在 Config 類中使用 @PropertySource 呼叫它的屬性檔案。然后在我想訪問其中一個值時使用 Environment.getProperty() 方法。
我該如何呼叫我當前正在使用的 Web 服務的特定屬性檔案?
配置類
@PropertySource(value = "file:src/main/resources/sddv1.properties", name = "sample.props")
@Configuration
public class Config {
@Autowired
Environment env;
@Bean
public void doStuff() {
String prop1 = env.getProperty(key1);
// I want to loop through different webservices and use the method above on each of their properties files
}
}
uj5u.com熱心網友回復:
使用標準應用程式屬性或使用組態檔。
https://docs.spring.io/spring-boot/docs/2.6.8/reference/htmlsingle/#features.profiles
或者只是用環境變數覆寫定義的屬性。
https://docs.spring.io/spring-boot/docs/2.6.8/reference/htmlsingle/#features.external-config
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/491368.html
