2016-10-26 7 views
1

tl; dr : 활성 프로필을 제공하면서 주석 기반 구성 클래스를 기반으로 Spring 컨텍스트를 만드는 방법은 무엇입니까?프로그래밍 방식으로 Spring 컨텍스트를 만들 때 활성 프로파일을 설정하는 방법은 무엇입니까?

주석을 사용하여 지정된 구성으로 클래스를 사용하여 Spring 컨텍스트를 만들려고합니다.

org.springframework.context.ApplicationContext context = 
    new org.springframework.context.annotation.AnnotationConfigApplicationContext(com.initech.ReleaserConfig.class); 

그것이 필요한 콩을 찾을 수 있기 때문에 충돌이 시작되지만 그 콩이 존재하지만 : 만 특정 프로필 "myProfile"에서이기는하지만.

활성 프로필을 어떻게 지정합니까? 하나의 org.springframework.context.annotation.AnnotationConfigApplicationContext(org.springframework.beans.factory.support.DefaultListableBeanFactory) 생성자를 사용해야한다는 느낌이 들지만 어떤 것이 적절한 지 모르겠습니다.


PS- 나는 봄 부팅을 사용하지 않지만 봄 4.2.4.RELEASE입니다. 애플리케이션 컨텍스트를 인스턴스화하기 때문에 작동하지 않습니다이 트릭을 할해야

답변

3

...

final AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(); 
appContext.getEnvironment().setActiveProfiles("myProfile"); 
appContext.register(com.initech.ReleaserConfig.class); 
appContext.refresh(); 
+0

예외 그래서 나는'.getEnvironment()'호출에 얻을 수 없다됩니다. – ArtB

+0

당신의 대답이 솔루션으로 이뤄져서 작동합니다. – ArtB

+0

@ArtB 컨텍스트를 인스턴스화하기 전에'System.setProperty ("spring.profiles.active", "myProfile");'를 사용할 수 있습니다. – xedo