【发布时间】:2020-11-19 22:42:32
【问题描述】:
有一些属性,我想间隔读取这些属性,如果属性有任何更新,我会在此基础上采取一些行动。
@Value("${sms.smpp.country.list:{}}")
private String smppCountryList;
@Value("${sms.smpp.country.config.list:{}}")
private String smppCountryConfigList;
@Value("${sms.smpp.properties.file.location:{}}")
private String propertiesFileLocation;
我已经编写了一个代码来使用 cron 读取属性,这里是代码。
@Scheduled(cron = "0 0/7 * * * ?")
public Properties readPropertiesFile() {
Properties prop = null;
try (InputStream input = new FileInputStream(propertiesFileLocation)) {
prop = new Properties();
prop.load(input);
} catch (IOException ex) {
logger.info("[SMPP] [CONFIG CRON] Exception occurred while reading the properties file: [{}]",ex.getMessage());
}
return prop;
}
代码被 cron 调用,或者我也可以在文件上写一个 onFileChangeHandler。
但是有什么简单而优雅的方法可以做到这一点,比如spring可以处理吗?
【问题讨论】:
-
在您当前的解决方案中,
@Value注解的属性是否会自动刷新?我会说不,但我想知道。 -
@RUARO Thibault 不,这些值没有被刷新,但在读取属性文件后我正在更新这些值。
标签: java spring properties-file