How to reload properties with Spring?

前端 未结 6 901
甜味超标
甜味超标 2020-12-01 12:44

I\'m using properties file with Spring 3. When Spring initializes its contex it loads the properties file and puts it in all beans with @Value annotation.

I want to

6条回答
  •  -上瘾入骨i
    2020-12-01 13:34

    use apache common with spring as follow:

    @Component
    public class ApplicationProperties {
        private PropertiesConfiguration configuration;
    
        @PostConstruct
        private void init() {
            try {
                String filePath = "/opt/files/myproperties.properties";
                System.out.println("Loading the properties file: " + filePath);
                configuration = new PropertiesConfiguration(filePath);
    
                //Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
                FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
               fileChangedReloadingStrategy.setRefreshDelay(60*1000);
                configuration.setReloadingStrategy(fileChangedReloadingStrategy);
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    
        public String getProperty(String key) {
            return (String) configuration.getProperty(key);
        }
    
        public void setProperty(String key, Object value) {
            configuration.setProperty(key, value);
        }
    
        public void save() {
            try {
                configuration.save();
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题