こんにちは、Spring Profiles を使用する簡単な例を書き留めようとしています。彼女はコードです。
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("kindergarten");
ctx.load("classpath:profile/*-config.xml");
ctx.refresh();
FoodProviderService foodProviderService =
ctx.getBean("foodProviderService", FoodProviderService.class);
List<Food> lunchSet = foodProviderService.provideLunchSet();
for (Food food: lunchSet) {
System.out.println("Food: " + food.getName());
}
しかし、GenericXmlApplicationContext の API に getEnviroment() メソッドがないため、3 行目に奇妙なことが起こります。
ctx.getEnvironment().setActiveProfiles("kindergarten");
私のSTSは、これがsintaxエラーであると考えているため、メインの実行を拒否し、次のメッセージを表示します。
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getEnvironment() is undefined for the type GenericXmlApplicationContext
何か案は ?ありがとう。