user5245796:
私は私のapplication.propertiesで次のプロパティを持っているとしましょう
url.2019=http://example.com/2019
url.2020=http://example.com/2020
そして、私はこの方法を持っています、
public String getUrl(String year) {
String url;
// here I want to read the property value based on the value of year
// if year is "2019", I want to get the value of ${url.2019}
// if year is "2020", I want to get the value of ${url.2020}
// something like #{url.#{year}} ??
return url;
}
これを達成するための最良の方法は何ですか?
ありがとうございました。
Shaileshチャンドラ:
これを達成するための複数の方法があるかもしれません
あなたの特性は、スプリングによって管理されていない場合
https://www.baeldung.com/inject-properties-value-non-spring-class
それは、スプリングによって管理されている場合
1.)あなたが欲しい方財産あなたのコードの読み取りにマップを注入することができ、application.properiesでマップを定義することができます
2)あなたは、環境変数を注入し、オンデマンドでプロパティを読み取ることができます
@Autowired
private Environment environment;
public String getUrl(String year) {
String url = "url." + year ;
String value =environment.getProperty(url);
return url;
}