Java Properties로 구성정보 관리

유방현·2024년 10월 28일

1. Properties 클래스 개요

1.1 정의

  • Map 인터페이스의 구현체
  • 키-값 쌍으로 구성 정보 저장
  • 프로그램 설정 관리에 특화

1.2 주요 특징

Properties properties = new Properties();
// 키-값은 모두 문자열로 저장
properties.setProperty("key", "value");
String value = properties.getProperty("key");

2. Properties 파일 저장

2.1 기본 저장 방법

public class PropertiesSaveExample {
    public void saveProperties() {
        Properties props = new Properties();
        
        // 설정 정보 추가
        props.setProperty("db.url", "jdbc:mysql://localhost:3306/mydb");
        props.setProperty("db.user", "admin");
        props.setProperty("db.password", "1234");
        
        try (FileWriter writer = new FileWriter("config.properties")) {
            // 파일로 저장
            props.store(writer, "Database Configuration");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.2 저장 형식

# Database Configuration
# Wed Oct 25 10:30:00 KST 2024
db.url=jdbc:mysql://localhost:3306/mydb
db.user=admin
db.password=1234

3. Properties 파일 로딩

3.1 기본 로딩 방법

public class PropertiesLoadExample {
    public void loadProperties() {
        Properties props = new Properties();
        
        try (FileReader reader = new FileReader("config.properties")) {
            // 파일에서 속성 로드
            props.load(reader);
            
            // 속성값 읽기
            String url = props.getProperty("db.url");
            String user = props.getProperty("db.user");
            String password = props.getProperty("db.password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.2 기본값 설정

public class PropertiesDefaultExample {
    public void getPropertyWithDefault() {
        Properties props = new Properties();
        
        // 속성이 없을 경우 기본값 사용
        String language = props.getProperty("app.language", "en");
        String timeout = props.getProperty("app.timeout", "30");
    }
}

4. 실전 활용 예제

4.1 애플리케이션 설정 관리

public class AppConfigManager {
    private static Properties config = new Properties();
    
    public static void loadConfig() {
        try (FileReader reader = new FileReader("app.properties")) {
            config.load(reader);
        } catch (IOException e) {
            System.err.println("설정 파일을 로드할 수 없습니다.");
        }
    }
    
    public static String getConfig(String key) {
        return config.getProperty(key);
    }
    
    public static void setConfig(String key, String value) {
        config.setProperty(key, value);
    }
    
    public static void saveConfig() {
        try (FileWriter writer = new FileWriter("app.properties")) {
            config.store(writer, "Application Settings");
        } catch (IOException e) {
            System.err.println("설정을 저장할 수 없습니다.");
        }
    }
}

4.2 다국어 지원

public class LanguageManager {
    private Properties languageProps = new Properties();
    
    public void loadLanguage(String language) {
        try (FileReader reader = new FileReader(language + ".properties")) {
            languageProps.load(reader);
        } catch (IOException e) {
            System.err.println("언어 파일을 로드할 수 없습니다.");
        }
    }
    
    public String getMessage(String key) {
        return languageProps.getProperty(key, "Message not found");
    }
}

5. 모범 사례와 주의사항

5.1 모범 사례

  1. 구조화된 키 이름 사용
# 계층적 구조
database.url=jdbc:mysql://localhost:3306/mydb
database.user=admin
database.password=1234

# 기능별 구분
ui.theme=dark
ui.language=ko
ui.fontSize=12
  1. 주석 활용
# 데이터베이스 연결 설정
database.url=jdbc:mysql://localhost:3306/mydb

# 사용자 인증 정보
database.user=admin

5.2 주의사항

  • 민감한 정보 관리
  • 파일 경로 관리
  • 인코딩 설정
  • 동시성 고려

0개의 댓글