JAVA

[config.properties] 파일 경로 관리

amoomar 2025. 3. 5. 16:22
반응형
log.base.path=logs/
webdriver.chrome.driver=C:/Users/wizai/Downloads/chromedriver-win64/chromedriver.exe
login.url=http://localhost:8082/comis4/uis/common/index.do
screenshot.url=http://localhost:8082/comis4/uis/amis/obs/windprofiler/windprofiler.do
screenshot.output.dir=C:/screenshots/

유지보수를 용이하도록 하기 위해서 java로 정의된 웹 어플리케이션 프로젝트에 경로 설정등을 정의한 별도 properties파일을 생성하여 관리 및 활용할 수 있는 내용에 대해 정리해보았다.

 

개발환경과 운영환경의 경우 참조해야하는 경로가 달라질 것을 대비하여 환경별로 적용할 properties를 별도 생성 및 관리하도록 하였다.

 

 

 

 

 

목차


     

     

     

     

    config.properties

    resources폴더에 각 설정파일을 운영용(config.properties)과 개발용(config_dev.properties)로 구분하여 생성해주었다.

     

     

    파일의 내부는 아래와 같이 하였다.

    클래스 파일에서 좌측 변수명 호출로 해당 변수의 값에 접근하려는 목적이며, 변수명을 통해 어떤 경로를 나타내는 것인지를 명확히 하기위해 온점(.)을 포함하여 가급적 상세하게 남겨주었다. 

    log.base.path=logs/
    webdriver.chrome.driver=C:/Users/wizai/Downloads/chromedriver-win64/chromedriver.exe
    login.url=http://localhost:8082/comis4/uis/common/index.do
    screenshot.url=http://localhost:8082/comis4/uis/amis/obs/windprofiler/windprofiler.do
    screenshot.output.dir=C:/screenshots/

     

     

     

     

    ConfigManager.java

    운영/개발 환경 구분별로 각 properties파일을 호출하고, 그설정 파일의 값을 가져올 수 있도록 하는 관리 클래스이다.

    package com.app;
    
    import com.Application;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.logging.Logger;
    
    public class ConfigManager {
        private static Properties properties = new Properties();
        private static boolean isDev = true; //개발, 운영 여부
    
        static {
            try {
                FileInputStream fis = null;
                if(isDev){ //개발용 파일 호출
                    fis = new FileInputStream("src/main/resources/config_dev.properties");
                }else{ //운영용 파일 호출
                    fis = new FileInputStream("src/main/resources/config.properties");
                }
                properties.load(fis);
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException("Failed to load config.properties: " + e.getMessage());
            }
        }
    
        // get만 정의
        public static String get(String key) {
            return properties.getProperty(key);
        }
    }

     

     

     

    활용예시

    // 활용법
    ConfigManager.get("호출할 config.properties내 변수")
    
    // 예시
    ConfigManager.get("webdriver.chrome.driver")

     

     

     

    해당 기법을 활용한 프로젝트의 github링크를 하단에 첨부하였다.

    https://github.com/Hamjeonghui/autoCapture

     

    GitHub - Hamjeonghui/autoCapture: 자동캡처

    자동캡처. Contribute to Hamjeonghui/autoCapture development by creating an account on GitHub.

    github.com

     

     

    반응형