スプリングブートサービスでapplication.ymlからカスタムコンフィグレーションをロードしています。 application.ymlマイカスタムクラスの国が2つのフィールドが含まれて スプリングブートアプリケーションのカスタムymlファイルからコンフィグレーションをロードできません
@Component
@ConfigurationProperties("app")
public class ContinentConfig {
private Map<String, List<Country>> continents = new HashMap<String, List<Country>>();
//get/set/tostring methods
}
、
public class Country {
String name;
String capital;
//get/set/tostring methods
}
は、私は以下のように持って、私は以下のようにBeanクラスで注釈を付けている
、
app:
continents:
Europe:
- name: France
capital: Paris
Asia:
- name: China
capital: Beijing
上記の設定で、私はapplication.ymlから設定をロードすることができます。
私は今、同じのsrc /メイン/リソースフォルダ内の別のcontinentconfig.ymlに設定を抽出します。だから、カスタム設定をcontinentconfig.ymlに移動して、のような他のプロパティを残しました。application.ymlのserver.port。
continentconfig.ymlの内容は、前述のapplication.ymlと同じです。
はまた、私は設定がContinentConfig Beanにcontinentconfig.ymlからロード取得されていない参照、この変更の後、ContinentConfigクラスに
@Component
@ConfigurationProperties("app")
@EnableConfigurationProperties
@PropertySource(value="classpath:continentconfig.yml")
public class ContinentConfig {
}
を以下の注釈を追加しました。
誰かが問題の解決にお手伝いできますか?
ご清聴ありがとうございます。しかし、独自のymlファイルにカスタム設定を外部化するもう1つの方法があります。おそらく、それをプロファイル固有のymlファイルとして使用します。 – juser
'application.yml'で基本設定を行い、必要なプロファイル' application-dev.yml'などでそれを上書きすることができます。 'YamlPropertySourceLoader'を使うことでもう一つの回避策があります。 https://stackoverflow.com/questions/21271468/spring-propertysource-using-yaml –