使用下面的bean每1秒重新加载config.properties。
@Componentpublic class PropertyLoader { @Autowired private StandardEnvironment environment; @Scheduled(fixedRate=1000) public void reload() throws IOException { MutablePropertySources propertySources = environment.getPropertySources(); PropertySource<?> resourcePropertySource = propertySources.get("class path resource [config.properties]"); Properties properties = new Properties(); InputStream inputStream = getClass().getResourceAsStream("/config.properties"); properties.load(inputStream); inputStream.close(); propertySources.replace("class path resource [config.properties]", new PropertiesPropertySource("class path resource [config.properties]", properties)); }}你的主要配置如下所示:
@EnableScheduling@PropertySource("classpath:/config.properties")public class HelloWorldConfig {}而不是使用@Value,每次你想要使用的最新属性
environment.get("my.property");注意
尽管示例中的config.properties是从类路径中获取的,但是它仍然可以是已添加到类路径中的外部文件。



