java - 如何在运行时更新 Spring Boot 应用程序的配置而不重新加载整个 ApplicationContext

标签 java spring configuration spring-boot

我想弄清楚如何在不重新启动整个应用程序的情况下动态更新/重新加载 Spring Boot 应用程序中的外部化配置。

大多数建议涉及在更改外部化配置后重新加载 ApplicationContext,但这相当于重新启动整个应用程序,所以这并不是那么有用。

翻阅SpringBoot引用文档,找到一章23.7 Typesafe Configuration Properties .

如果我理解正确,这允许定义简单的 POJO 类,将您的应用程序(外部化)配置值保存为属性。

至少在理论上,此方案可用于将 bean 绑定(bind)到所需的配置 POJO 一次,并且在配置更改时仅更新 POJO 中的值。组件可以在下次访问 POJO 上的 getter 时轻松获取更改...

但是,我还没有想出如何启用这种行为。当相关配置发生变化时,是否有一些明显的方法可以动态更新用 @ConfigurationProperties 注释的组件?

最佳答案

听起来您正在寻找 Spring Cloud 提供的 @RefreshScope。来自Spring Cloud documentation :

A Spring @Bean that is marked as @RefreshScope will get special treatment when there is a configuration change. This addresses the problem of stateful beans that only get their configuration injected when they are initialized. For instance if a DataSource has open connections when the database URL is changed via the Environment, we probably want the holders of those connections to be able to complete what they are doing. Then the next time someone borrows a connection from the pool he gets one with the new URL.

关于java - 如何在运行时更新 Spring Boot 应用程序的配置而不重新加载整个 ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33365874/

相关文章:

java - Java程序执行过程中在文件中保存大量字符串

configuration - Octave :禁用 Octave 工作区崩溃文件

java - WebDriver 找不到 - 是 [object XrayWrapper [object Text]

java - Eclipse 中的快速编译器错误消息

java - 注解 @Column( nullable = false ) vs @NotNull 区别

java - Spring :返回@ResponseBody "ResponseEntity<List<JSONObject>>"

java - Spring Hibernate查询与非主键的一对一关系

php - 在 php.ini 中默认将 FTP 设置为被动模式

linux - 需要帮助理解 tsocks FAQ 中的一行

java - Java中如何提取字符串