【发布时间】:2021-01-08 20:52:31
【问题描述】:
我正在使用 Spring Boot,并在我的应用程序启动时在 @Configuration bean 中加载/生成应用程序属性,并将它们放置在 HashMap 中,例如:
@Configuration
public class Config {
@Bean(name = "appConfig")
public Map<String, String> getApplicationConfig() {
Map<String, String> appConfig = new HashMap<>();
//load or generate global config varibles
appConfig.put("GLOBAL_CONFIG", "VALUE 1");
return appConfig;
}
我在几个 bean 中注入了这个属性映射,例如:
@RestController
@RequestMapping("/getglobalconfig")
public class ConfigController {
@Value("#{appConfig}")
private Map<String, String> appConfig;
@GetMapping
public String getGlobalConfig() { return appConfig.get("GLOBAL_CONFIG"); }
}
在某些事件之后,我想以线程安全的方式更改例如我的 appConfig HashMap GLOBAL_CONFIG 值的值,以便在所有注入的 bean 中更新它,例如在上面的 ConfigController 示例中。
以线程安全的方式完成此任务并让我的 bean 重新加载值而不重新初始化它们或重新启动应用程序的最佳模式是什么? Spring cloud config 看起来很有趣,但不幸的是我无法使用它。
我不限于 HashMap 属性,我只需要线程安全地重新加载全局属性,以便在重新加载后在注入的 bean 中可见。
【问题讨论】:
标签: java spring multithreading spring-boot concurrency