blah blah

yaml 을 spring 에서 사용하기

스프링 프레임워크 쪽에서는 공백 문자로 설정 분류하는 것이 모호하다며 지원할 계획이 없는듯, 하지만 spring-boot 에서는 yaml 포맷의 설정 파일을 이용하는 듯 해서 여기에서 구현해놓은걸 그대로 가져다 써보기로 했다.

참조 소스 : https://github.com/cloudfoundry/uaa/tree/master/common/src/main/java/org/cloudfoundry/identity/uaa/config

test1.yml

bill-to: &id001
   given : Chris
   family : Dumars
   address:
       city : Royal Oak
       state : MI
       postal : 48046
ship-to: *id001
product:
   - sku :
        item1: abc
        item2: efg
     quantity : 4
     description : Basketball
   - sku : BL4438H
     quantity : 1
     description : Super Hoop
 taxyn : false
 tax : 251.42
 total: 4443.52

applicationContext.xml

<bean id="ymlFactoryBean"
      class="org.cloudfoundry.identity.uaa.confi.YamlPropertiesFactoryBean">
   <property name="resources">
      <list>
         <value>classpath:test1.yml</value>
         <value>...</value>
      </list>
   </property>
 </bean>

테스트

ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);

Properties yml = (Properties) context.getBean(“ymlFactoryBean”);
System.out.println(“# date : ” + yml.get(“date”));
System.out.println(“# bill-to.given : ” + yml.getProperty(“bill-to.given”));
System.out.println(“# product[0].sku.item1 : ” + yml.getProperty(“product[0].sku.item1”));
System.out.println(“# product[1].sku : ” + yml.getProperty(“product[1].sku”));
System.out.println(“# ship-to.give : ” + yml.getProperty(“ship-to.given”));

boolean taxYn = (Boolean) yml.get(“taxyn”);
if(taxYn) {
     System.out.println(“# tax : ” + yml.get(“tax”));
}

아직 전체적인 구조 이해를 못해서 그리 깔끔한 방법은 아니지만 일단 …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s