0

こんにちは、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

何か案は ?ありがとう。

4

1 に答える 1

1

3.0.6.RELEASE Spring バージョンがあったようですが、プロファイルが 3.1.0 バージョンから追加されたので、pom.xml に移動してこれを変更するだけで済みます。

    <spring.framework.version>3.0.6.RELEASE</spring.framework.version>

これに

        <spring.framework.version>3.1.0.RELEASE</spring.framework.version>
于 2012-08-30T10:33:06.310 に答える