자바 프로퍼티(Java Properties) 테스트 :: 자바일반[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

자바일반
[1]
등록일:2018-09-10 10:11:40 (0%)
작성자:
제목:자바 프로퍼티(Java Properties) 테스트

간단하게 자바 프로퍼티Java Properties에 대해 테스트 해봤다.

1. Setup

# application.properties
demo.value=test
demo.type=dev
# application-prod.properties
demo.type=production

2. Java Code

import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

public class PropertiesDemo {

    private Properties properties;

    public PropertiesDemo() {
        properties = new Properties();
    }

    public Properties getProperties() {
        return properties;
    }

    /**
     * 이 메소드는 프로퍼티 파일을 스트림으로 읽어 들여 멤버 변수의 프로퍼티에 적재합니다.
     *
     * @param path
     * @throws IOException
     */
    public void loadProp(String path) throws IOException {

        InputStream inputStream = getClass().getResourceAsStream(path);
        properties.load(inputStream);
        inputStream.close();
    }

    /**
     * 이 메소드는 static으로 선언해서 프로퍼티 파일을 읽는 것을 보여줍니다.
     * @param path
     * @return
     * @throws IOException
     */
    public static Properties loadPropForStatic(String path) throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = PropertiesDemo.class.getClassLoader().getResourceAsStream(path);
        properties.load(inputStream);
        inputStream.close();
        return properties;
    }

    public static void main(String[] args) throws IOException {
        PropertiesDemo propertiesDemo = new PropertiesDemo();

        // 프로퍼티 파일을 읽어들이고 해당 값을 출력해봅니다.
        propertiesDemo.loadProp("/application.properties");
        Properties properties = propertiesDemo.getProperties();
        properties.list(System.out);

        // 아래 코드는 새로운 프로퍼티 파일에 같은 키를 준 경우 오버라이드 하는 것을 확인합니다.
        Properties staticProp = PropertiesDemo.loadPropForStatic("application-prod.properties");
        properties.putAll(staticProp);
        properties = propertiesDemo.getProperties();
        properties.list(System.out);

        // 아래 코드는 프로퍼티간의 결합을 확인합니다.
        Properties dummy = new Properties();
        dummy.put("demo.type", "dummy"); // 기존 키를 오버라이드 합니다.
        dummy.put("demo.temp", "temp"); // 새로운 키를 추가합니다.
        properties.putAll(dummy); // 기존 프로퍼티에 더미 프로퍼티를 결합합니다.
        properties.list(System.out);

        // 아래 코드는 개별 키를 주어 값을 반환받습니다.
        Object type = properties.get("demo.type");

        // 아래코드는 프로퍼티를 키들을 순회하는 구문입니다.
        // .stringPropertyNames 대신 .keySet 을 사용할수도 있습니다.
        for (String key : properties.stringPropertyNames()) {
            Object value = properties.getProperty(key);
        }

        // 해당 키가 있는지 여부를 판별합니다.
        properties.containsKey("demo.type");

        // 해당 값이 있는지 여부를 판별합니다.
        properties.containsValue("dummy");

        // 보너스 코드
        // 키값을 주고 해당 값이 있으면 해당 값을 반환하지만
        // 해당 값이 없으면 null을 반환합니다.
        Object result = properties.computeIfAbsent("undefined", value -> returnNull(value));
        System.out.println("result value is " + result);
    }

    public static Object returnNull(Object key) {
        System.out.println(key + " value is null.");
        return null;
    }
}

3. Result

-- listing properties --
demo.type=dev
demo.value=test
-- listing properties --
demo.type=production
demo.value=test
-- listing properties --
demo.type=dummy
demo.value=test
demo.temp=temp
undefined value is null.
result value is null

[본문링크] 자바 프로퍼티(Java Properties) 테스트
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=34817
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.