2015년 12월 10일 목요일

프로파일(profile) 속성을 이용한 설정

동일한 스프링 빈을 여러개 만들어 놓고 상황(환경)에 따라서 적절한 스프링 빈을 사용할 수 있다. profile 속성을 사용하면 된다.

setActiveProfiles(profile); 을 이용하여 컨텍스트에 대하여 다른 스프링 빈을 사용할 수 있다.


  1. String resources = "classpath:applicationContext.xml";  
  2. String resources2 = "classpath:applicationContext2.xml";  
  3. GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();  
  4. ctx.getEnvironment().setActiveProfiles("dev");  
  5. ctx.load(resources, resources2);  
  6. ctx.refresh();  
  7.   
  8. /* do something */  
  9.   
  10. ctx.close();  

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.     profile="dev">  
  7.     <bean id="myCalculator" class="com.devchun.TwoNumber">  
  8.         <property name="num1" value="10" />  
  9.         <property name="num2" value="30" />  
  10.     </bean>  
  11. </beans>  

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.     profile="run">  
  7.     <bean id="myCalculator" class="com.devchun.TwoNumber">  
  8.         <property name="num1" value="90" />  
  9.         <property name="num2" value="120" />  
  10.     </bean>  
  11. </beans> 


위와 같이 두개의 스프링 빈(xml)에 대하여 profile 설정함에 따라 각기 다른 스프링 빈을 사용할 수 있다.

댓글 없음:

댓글 쓰기