프로퍼티의 변경을 감지하여 바로 반영되게 하고 값을 가져올 수 있는 방법

  1. 필요한 라이브러리를 가져옵니다.
    http://mavenrepository.com/artifact/org.apache.commons/commons-configuration2
    http://mavenrepository.com/artifact/commons-beanutils/commons-beanutils
    compile ('org.apache.commons:commons-configuration2:2.1')
    compile ('commons-beanutils:commons-beanutils:1.9.3')
    
  2. 프로퍼티 설정 파일을 생성합니다.
    import org.apache.commons.configuration2.Configuration;
    import org.apache.commons.configuration2.PropertiesConfiguration;
    import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
    import org.apache.commons.configuration2.builder.fluent.Parameters;
    import org.apache.commons.configuration2.ex.ConfigurationException;
    import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;
    import org.springframework.stereotype.Component;
    import javax.annotation.PostConstruct;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    @Component  
    public class Properties {
        private ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder;
    	
        @PostConstruct
        void init() {
            Parameters params = new Parameters();
            builder = new ReloadingFileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
                         .configure(new params.fileBased().setFile(new File("../src/main/resources/messageCommon.properties")));
            PeriodicReloadingTrigger configReloadingTrigger = new PeriodicReloadingTrigger(
            builder.getReloadingController(), null, 3, TimeUnit.SECONDS);
            configReloadingTrigger.start();
        }
        
        public Configuration getCompositeConfiguration() {
            try {
                return builder.getConfiguration();
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public List<String> getKeys() {
           Iterator<String> keys = getCompositeConfiguration().getKeys();
           List<String> keyList = new ArrayList<String>();
           while(keys.hasNext()) {
               keyList.add(keys.next());
           }
           return keyList;
        }
    
        public String getString(String key) {
           return getCompositeConfiguration().getString(key);
        }
    
        public int getInt(String key) {
           return getCompositeConfiguration().getInt(key);
        }
    }
    

    참고(Reloading File-based Configurations) - https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html
    builder.getReloadingController(), null, 1, TimeUnit.SECONDS) - 변경 감지를 시간을 설정
    getKeys() - 프로퍼티의 모든 키를 가져옴
    getString()- 프로퍼티의 키를 이용해서 값을 가져옴

  3. 값 가져오기
    public class TestService {
    
        @Autowired
        private Properties properties;
    
        try {
            String value = properties.getString("가져올 프로퍼티 키")
        } catch (Exception e) {
        ....
    }