2013년 7월 4일 목요일

[EtC] Spring3.x - properties 파일을 xml 로 바꾸자.



원문 : http://umsh86.tistory.com/category/spring


1. 설정 값이 들어 있는 xml  파일. 이 문서에서는 mobileWeb-properties.xml 파일.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd" >

<properties>
  <comment>comment here</comment>
  <entry key="TestMessage">이 값은 mobileWeb-properties.xml 에 들어 있는 값 입니다.</entry>
</properties>


2. beans가 등록된 xml 파일. 이 문서에서는 mobileWeb-servlet.xml 파일.
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
                      ...중략...
              xmlns:util="http://www.springframework.org/schema/util"
              xsi:schemaLocation="http://www.springframework.org/schema/beans"
                      ...중략...
              http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util.xsd"
             >

<!-- 설정값이 들어 있는 xml 파일  -->
<util:properties id="config" location="/WEB-INF/config/mobileWeb-properties.xml"></util:properties>


3. xml 문서에서 값을 가져다 쓰기.
   <property name="myValue" value="#{config['TestMessage']}">
       ... 중략 ...
   </property>


4. jsp 에서 값을 가져다 쓰기.
<!-- xml의 설정 값을 읽어 오기 위한 부분 -->
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    ... 중략 ...
<body>
    ... 중략 ...
 <spring:eval expression="@config['TestMessage']"></spring:eval><br>
</body>


5. SpEL을 이용해서 Java에서 사용하는 방법.
@Value("#{config['TestMessage']}") String TestMessage;