简体   繁体   English

如何在运行时更新SpringBoot应用程序的配置,而无需重新加载整个ApplicationContext

[英]How to update configuration of SpringBoot application at runtime without reloading whole ApplicationContext

I am trying to figure out how can I dynamically update/reload externalized configuration in a Spring Boot application without restarting the whole application. 我试图找出如何在Spring Boot应用程序中动态更新/重新加载外部化配置,而无需重新启动整个应用程序。

Most of the advice involves reloading ApplicationContext after changing the externalized configuration, but that is equivalent to restarting the entire application, so this is not really all that useful. 大多数建议涉及在更改外部化配置后重新加载ApplicationContext,但这相当于重新启动整个应用程序,因此这并非真正有用。

Reading through SpringBoot reference documentation, I found a chapter 23.7 Typesafe Configuration Properties . 通过SpringBoot参考文档阅读,我找到了第23.7Typesafe配置属性

If I understand it correctly, this allows to define simple POJO classes that will hold your application (externalized) configuration values as attributes. 如果我理解正确,这允许定义简单的POJO类,它将您的应用程序(外部化)配置值保存为属性。

In theory at least, this scheme could be used to bind beans only once to the required configuration POJO and upon configuration change just update the values in the POJO. 至少在理论上,此方案可用于仅将bean绑定到所需的配置POJO,并且在配置更改时只更新POJO中的值。 Components could easily pick up the changes next time they access getters on the POJO... 组件可以在下次访问POJO上的getter时轻松获取更改...

However, I have yet not managed to figure out how to enable this type of behavior. 但是,我还没有设法弄清楚如何启用这种行为。 Is there some glaringly obvious way to dynamically update components annotated with @ConfigurationProperties when relevant configuration has changed? 当相关配置发生变化时,是否有一些明显的方法可以动态更新使用@ConfigurationProperties注释的组件?

It sounds like you're looking for @RefreshScope which is provided by Spring Cloud. 听起来你正在寻找由Spring Cloud提供的@RefreshScope From the Spring Cloud documentation : Spring Cloud文档

A Spring @Bean that is marked as @RefreshScope will get special treatment when there is a configuration change. 当配置发生变化时,标记为@RefreshScope Spring @Bean将得到特殊处理。 This addresses the problem of stateful beans that only get their configuration injected when they are initialized. 这解决了有状态bean的问题,只有在初始化时才会注入其配置。 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. 例如,如果DataSource在通过Environment更改数据库URL时具有打开的连接,我们可能希望这些连接的持有者能够完成他们正在做的事情。 Then the next time someone borrows a connection from the pool he gets one with the new URL. 然后,当有人从池中借用连接时,他会获得一个带有新URL的连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关闭ApplicationContext仍然没有关闭springboot应用 - Closing ApplicationContext still not shutting down springboot application 为什么获取 springboot 应用程序的 applicationContext 很复杂? - Why it's complex to get the applicationContext of a springboot application? 如何在运行时获取 Springboot 应用的 WebApplicationType? - How to get the WebApplicationType of Springboot application at runtime? 如何加载外部属性文件并覆盖springboot application.properties(没有运行时参数)? - How to load external properties file and override springboot application.properties (without runtime args)? 将 Elasticsearch 配置为 java SpringBoot 应用程序 - configuration Elasticsearch to java SpringBoot Application 使用application.yml配置SpringBoot - SpringBoot Configuration with application.yml SpringBoot微服务如何使用java配置在应用上下文中设置属性 - SpringBoot microservice How to set properties in application context using java configuration spring 引导如何使用 @Autowired 没有 @Component 注释和 xml 配置注入 ApplicationContext 和 JdbcTemplate 的实例? - How does spring boot inject the instance of ApplicationContext and JdbcTemplate using @Autowired without @Component annotation and xml configuration? 修改活动配置文件并在Spring Boot应用程序中刷新ApplicationContext运行时 - Modify active profiles and refresh ApplicationContext runtime in a Spring Boot application 如何在不启动整个上下文的情况下测试 Spring 中的@Configuration class? - How to test @Configuration class in Spring without starting up whole context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM