Properties Service

외부 파일이나 환경 정보에 구성되어 있는 key, value의 쌍을 내부적으로 가지고 있으며, 어플리케이션이 이 특정 key에 대한 value에 접근할 수 있도록 해주는 서비스이다. 이 서비스는 주로 시스템의 설치 환경에 관련된 정보나, 잦은 정보의 변경이 요구되는 경우 외부에서 그 정보를 관리하게 함으로써 시스템의 가변성을 향상시킨다. EJB 컴포넌트의 경우는 이미 이러한 정보를 관리할 수 있는 내부적인 기능을 제공하고 있으므로 서비스 내에서는 별도로 이 기능을 사용할 필요는 없다. 다음은 Anyframe Core에서 제공하는 Properties 서비스에 대한 구현체이다.

PropertiesServiceImpl

다음은 Properties 서비스를 사용하기 위해 필요한 설정 정보들이다.
Tag Name
Attrubute Name
Description
Required
Default Value
Child Tag
config:configuration filename properties
filename key, value의 쌍이 외부 별도 파일에 존재하는 경우 해당 파일명을 경로와 함께 표시한다. 절대 / 상대 물리적인 파일 경로 지정 방법과 Classpath를 이용한 지정 방법 2가지가 있다.
N
N/A
filename encoding property file의 encoding 정보를 정의한다.
N
NULL
properties Properties 서비스 속성 정의 파일 내에 직접 key, value의 쌍을 하위요소 element를 이용하여 지정한다.
N
N/A
element
element key 찾을 value에 대한 key.
Y
element value 해당 key에 대한 value.
Y

Samples

  • Configuration
  • 다음은 Properties 서비스의 속성을 정의한 applicationContext-properties.xml 의 일부이다. 아래 PropertiesService는 클래스패스 상에 존재하는 testcase-resource.properties 파일에 정의된 property들과 개별 정의된 property들을 관리하게 된다.
    <bean name="propertiesService" class="anyframe.core.properties.impl.PropertiesServiceImpl">
       <config:configuration>
          <fileName>classpath:services/properties/testcase-resource.properties</fileName>
    
          <properties>
             <element key="AAAA" value="1234"/>
             <element key="bbbb" value="4567"/>
             <element key="cccc" value="5678"/>
             <element key="PAGE_SIZE" value="10"/>
             <element key="PAGE_UNIT" value="10"/>
          </properties>
       </config:configuration>
    </bean>
  • TestCase
  • 다음은 앞서 정의한 속성 설정을 기반으로 Properties 서비스를 사용하는 PropertiesServiceTest.java 코드의 일부이다.
    /**
     * PropertiesService의 getString, getDouble을 통해 
     * String, double 타입의 데이터 값을 얻어오는 테스트
     */
    public void testPropertiesService() throws Exception {
        IPropertiesService propertiesService = (IPropertiesService) context
           .getBean("propertiesService");
        // 1. property element로 정의된 "AAAA"를 얻는다.
        if (propertiesService.getString("AAAA")==null)
              throw new Exception("fail to getString of propertiesService");
        // 2. properties에 정의되어있는 number.double의 값을 가지고 온다.
        if (!new Double(propertiesService.getDouble("number.double")).equals(new Double(1234)));
        }
    }

Sample Property File

다음은 위 Properties 서비스 속성 정의 파일에 정의된 testcase-resource.properties 파일의 내용이다.
# lines starting with # are comments
 
# This is the simplest property
key value

# A long property may be separated on multiple lines
longvalue aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

# This is a property with many tokens
tokens_on_a_line first token, second token

# This sequence generates exactly the same result
tokens_on_multiple_lines second token

# commas may be escaped in tokens
commas.excaped Hi, what'up?


# This is a property associated with number
number.int=1234
number.float=1234
number.double=1234
number.long=1234
       
boolean=true

include=./src/test/titan/services/properties/apps/resource2.properties
위 예에서 "testcase-resource.properties" 파일의 include key는 시스템 내부에서 내용하는 keyword이다. Keyword의 역할은 두 번째 파일 "resource2.properties"에 정의된 내용을 별도로 서비스의 속성 정보내에 추가하지 않아도 내부적으로 그 정보를 서비스 내로 읽어 들이도록 한다.
또한 value영역의 "\" 문자는 하나의 라인임을 의미하며, ","는 사용한 value의 나열을 해당 key에 대해 Array로 구성한다. ","를 사용하기 위해서는 "\,"과 같이 사용한다.

Resources

  • 다운로드
  • 샘플 테스트 코드를 포함하고 있는 systemierii-propertiestest-src.zip 파일을 다운받은 후, 테스트 환경 설정 을 참조하여 위에서 제시한 예제 코드를 실행해 볼 수 있다.
    Name
    Download
    anyframe-propertiestest-src.zip
    Download