IT_Programming/Dev Libs & Framework

[펌] spring profile 을 사용하여 환경에 맞게 deploy 하기

JJun ™ 2016. 2. 11. 04:31



 * 출처

 : https://www.lesstif.com/pages/viewpage.action?pageId=18220309




Spring profile 을 사용하여 환경에 맞게 deploy 하기




 

개요

spring 3.1 부터  포함된 기능으로 환경에 맞게 적합한 profile 을 사용할 수 있게 하는 기능. maven profile 과 비슷한 역할을 한다고 보면 됨.

 

사용


application Context 의 beans 설정에 다음과 같이 profile 을 지정

<beans profile="devel">   
     <bean id="myBean" class="com.example.demo.DevelBean">
        <property name="env" value="개발 profile"/>
    </bean>
</beans>
<beans profile="production">
    <bean id="myBean" class="com.example.demo.ProductionBean">
        <property name="env" value="운영 profile"/>
    </bean>
</beans>


profile 설정시 여러개의 profile 을 지정할수 있다

<beans profile="devel">  
     <bean id="myBean" class="com.example.demo.DevelBean">
        <property name="env" value="개발 profile"/>
    </bean>
</beans>
<beans profile="test,qa">  
     <bean id="myBean" class="com.example.demo.TestBean">
        <property name="env" value="test, qa profile"/>
    </bean>
</beans>

 

활성화


jvm property 로 전달

spring.profiles.active property 에 활성화할 프로파일명을 설정 (, 로 여러 개 설정 가능)

-Dspring.profiles.active=dev,profile2


web.xml 에 설정

web.xml 에 활성화할 프로파일 설정.

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>dev</param-value>
</context-param>


ApplicationContext 의 Environment 에 설정

@Autowired ConfigurableWebApplicationContext servContext;
     
    private void setProfile(String newProfile) {
        ConfigurableWebApplicationContext rootContext  = (ConfigurableWebApplicationContext) servContext.getParent();
         
        rootContext.getEnvironment().setActiveProfiles(newProfile);
        rootContext.refresh();                
          
        // Refreshing Spring-servlet WebApplicationContext
        servContext.getEnvironment().setActiveProfiles(newProfile);
        servContext.refresh();
    }


@ActiveProfiles annotation 사용

jUnit 등으로 test case 를 돌릴 경우에 유용

package com.bank.service;
 
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "classpath:/app-config.xml"
@ContextConfiguration("/app-config.xml")
@ActiveProfiles("dev")
public class TransferServiceTest {
 
    @Autowired
    private TransferService transferService;
 
    @Test
    public void testTransferService() {
        // test the transferService
    }
}

 

 

 

Ref