2012-08-30 2 views
0

안녕하세요, 저는 스프링 프로파일을 사용하는 간단한 예제를 작성하려고합니다.Spring getEnvironment()

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()); 
} 

} 그러나 이상한 점은

ctx.getEnvironment().setActiveProfiles("kindergarten"); 

투여하지 work.My STS 그가 생각하기 때문에 주요 실행을 거부 세 번째 줄 수 있도록 API의에서이 getEnviroment() 메소드가없는 선량 GenericXmlApplicationContext 발생 이것은 그가 나 에게이 메시지를주는 sintax 오류입니다 :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method getEnvironment() is undefined for the type GenericXmlApplicationContext 

어떤 아이디어? 덕분에 .

답변

1

답변을 찾았습니다. 3.0.6.RELEASE Spring 버전이지만 프로필은 3.1.0 버전에서 추가되었으므로 필요한 것은 모두 내 pom.xml로 이동하여 변경해야합니다. 이

 <spring.framework.version>3.1.0.RELEASE</spring.framework.version> 
으로

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

관련 문제