You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@ConfigurationProperties should have an option to have properties read each time from Environment #259
@ConfigurationProperties objects should have some mechanism for allowing some or all properties to be read from a propertySource on each get invocation. It seems like instead of injected a concrete implementation into the AppContext we should inject a proxy for the Config object which can reference the Environment abstraction.
The usecase for this would be when we have implemented a custom PropertySource which is aware of property changes.
Sounds like a wider scope than just @ConfigurationProperties to me. Doesn't the same feature potentially apply to any bean that can refresh itself on an environment change?
That is leaving aside the question of whether this is a good idea or not. Is it a good idea? In a world of stateless micro-services isn't it better to just change the config and restart the app? Or are we mainly talking about development time? What's the benefit exactly?
I am not sure if the original author is watching this or not.
I agree, this might not be a great idea. Dynamically reloading properties can be a tricky problem, often just updating the @ConfigurationProperties object won't be enough, you need more of an event based approach so that other classes are aware of the change.
I also can't see this being a very common use-case.
Perhaps we can add some hooks to allow a roll-your-own solution if 'getting on each invocation' is really needed?
Hey, I was not watching the issue from github, sorry about that. I'm the author of the request.
I do not believe this has anything to do with beans other than @ConfigurationProperties, or at least that isn't something I'd want. To give a little more background on what I'm doing, I've got an implementation of Netflix's Archaius (https://github.com/Netflix/archaius/wiki/Users-Guide) hidden under Spring's PropertySource. Archaius provides some great support for property change listeners, reloading properties, etc. that we use pretty heavily at Netflix, but being a Spring person I greatly prefer being able to use @Value, Environment, spel property resolution, and @ConfigurationProperties whenever possible. Hence the NetflixPropertySource implementation. Of course @Value doesn't work since its only reading the property once on startup/bean creation, and I don't think I'd want that to change honestly, but with @ConfigurationProperties would be much easier to implement delegation to the Spring Environment. I'd argue it should always delegate to the PropertySources, and if you're concerned about performance/caching let that be the concern of your PropertySource implementation.
As to whether or not reloadable configuration is a good idea in a world of micro cloud services, I'd just point out that this new cloud world isn't quite perfect yet. For us, rolling out new clusters into AWS (with sizes ranging from dozens to 100+ per app cluster) can take 45minutes just to get started and perhaps hours for caches to become warm. Because of that we often find it necessary to use feature flags based on properties to enable/disable functionality across our entire cloud infrastructure. This is something Archaius does rather nicely, and its just a shame at the moment I can't leverage @ConfigurationProperties as much as I'd like to for working with Archaius.
Any kind of hook to allow me to customize the creation of @ConfigurationProperties would be sufficient, but I do suggest considering delegation to the Spring Environment as an option, if not the default behavior.
We're very interested in this discussion, and the recent announcement about dynamic Spring props. We've built https://github.com/Capgemini/archaius-spring-adapter which sounds like it overlaps heavily with what @twicksell has been working on. We'd be interested in being involved in work in this area as it'd help us greatly on our current project.
Cheers @dsyer We'll take a look and see if we can contribute. We're big users of Spring (Boot especially), Camel, and various Netflix bits and pieces too. Anything which helps glue them together which is OSS is of interest to us.
I was wondering if someone could point me (and other readers) in the direction of the "recent announcement about dynamic Spring props" as mentioned by andrewharmellaw above. I was interested to hear what the latest thinking is and I cant seem to find anything recent.
Hmm, that seems to be documentation rather than an announcement as such. Can I assume in that case that the dynamic properties apply only to the Cloud and Boot products and there are no announced changes to the original Spring framework?
@dsyer Hi, I am having this issue with Spring Cloud: I added a MapPropertySource to the env, I am changing the values from the ConcurentMap from another thread, but I can't see those values reflecting in env.getProperty or in @ConfigurationProperties beans. Pls advise!
If you really changed the environment, you would see it in getProperty(), so if you didn't see it there, then I suspect you didn't change it, or you changed a property that is overridden in another property sourec. You can see all the property sources in the /env endpoint.
You have to rebind to get @ConfigurationProperties to update, so that's a different problem. There's a ContextRefresher in spring-cloud-context that you can use if you want to trigger it manually, or you can use the /refresh endpoint.
We've discussed the possibility of supporting a property that's a Supplier<?> that retrieves the value from the Environment each time get() is called. Our feeling is that this isn't a viable solution for two reasons:
For a frequently used infrequently changed piece of configuration, retrieving the property from the environment each time is inefficient
Retrieving a consistent view of updates to multiple properties is hard, if not impossible.
Our feeling, as it was above, is that an event-based approach would be better. The event would provide a complete new instance of a configuration properties class with the changed value or values. This addresses both of the problems described above while also working with immutable configuration properties that are currently in plan for 2.2.
The question remains if this is a good idea. We'd also be interested in hearing some of the use cases that a change in this area would enable. As things stand, there hasn't been particularly broad interest in this functionality. Given the reasonably large amount of complexity that it would introduce, it's difficult to justify implementing it at this time.
changed the title [-]@ConfigurationProperties should have an option to have properties read each time from Evironment [/-][+]@ConfigurationProperties should have an option to have properties read each time from Environment [/+]on Jul 1, 2019
Adding @DynamicConfig on @ConfigurationProperties simply works !
With Spring Boot native features leveraged, and with the file watcher and hot-reload mechanism implemented by only 400+ lines of code, @Value@ConfigurationProperties could always be the newest value.
This lib is widely used in 20+ core applications for more than half a year in the company I'm working for. Hope this feature becomes a built-in feature of Spring Boot. @philwebb@dsyer@wilkinsona Would you like to take a look at the source codes and put some suggestions? Thanks !
Great job. As of a suggestion I would remove the Lombok dependency and only
depend on spring codebase.
One question: this dynamic configuration only affects the given project?
What about share configuration between project? That what spring-config is
intended to do, while your project is intented to make the configurations
dynamic to the project code base. How about security?
Do you want to incorporate your codebase to spring's codebase? Of course,
if they consider it interesting.
Regards,
Flavio Oliva
This dynamic config only affects given project because it's an enhancement of spring boot config native ways.
Config sharing could be on orchestration layer, for eg. mount same ConfigMap as files in different Kubernetes workloads
As for security, there are conditional beans, so the config file will not be dynamic unless the owner intends to. Moreover, with GitOps workflow, the source of config files are in other systems like Git, thus, the security responsibilities are shifted to Git permission, CI/CD pipeline security, Kubernetes RBAC, etc.
Thanks very much for sharing the project @Code2Life. It looks like an interesting alternative to @RefreshScope for those that want it. I don't have any specific comments on the code (although I agree with @denon82 about Lombok). We're unlikely to get to this feature in Spring Boot itself for a little while so it's great that users have another option.
Thanks for reviewing @philwebb , @RefreshScope relies on spring cloud stack and additional config server. we've tried spring-cloud-config and other spring-cloud components in production years ago and found it's too heavyweight, then we went back to Spring Boot.
FYI spring-cloud-config isn't needed for @RefreshScope, and also @RefreshScope is not needed for @ConfigurationProperties. Just put spring-cloud-commons on the classpath - it's very lightweight, really - the only dependency is Spring Boot.
@RefreshScope may not be secure enough as it will first destroy the current bean and then create a new bean. What if the environment properties encounter an exception when creating a new bean? This online running application will be a disaster. Is it possible to adjust the order and first try creating a temporary bean? If it can be initialized normally, then replace the original bean. This will be a significant optimization for runtime configuration updates.
In addition,if it is possible to move @RefreshScope from spring-cloud to spring boot? it should not be bundled to spring cloud project.
I am a core builder in the Nacos community (https://nacos.io/en/) which is widely used by chinese developer community and I am responsible for config center.
We want to implement all configurations on the hosted application side, support elegant updates during runtime, and improve the efficiency of business software iteration. If Spring can support elegant refresh beans, then there is no need for every second party middleware to implement this feature
Activity
dsyer commentedon Jan 23, 2014
Sounds like a wider scope than just
@ConfigurationPropertiesto me. Doesn't the same feature potentially apply to any bean that can refresh itself on an environment change?That is leaving aside the question of whether this is a good idea or not. Is it a good idea? In a world of stateless micro-services isn't it better to just change the config and restart the app? Or are we mainly talking about development time? What's the benefit exactly?
philwebb commentedon Jan 23, 2014
I am not sure if the original author is watching this or not.
I agree, this might not be a great idea. Dynamically reloading properties can be a tricky problem, often just updating the
@ConfigurationPropertiesobject won't be enough, you need more of an event based approach so that other classes are aware of the change.I also can't see this being a very common use-case.
Perhaps we can add some hooks to allow a roll-your-own solution if 'getting on each invocation' is really needed?
twicksell commentedon Feb 7, 2014
Hey, I was not watching the issue from github, sorry about that. I'm the author of the request.
I do not believe this has anything to do with beans other than
@ConfigurationProperties, or at least that isn't something I'd want. To give a little more background on what I'm doing, I've got an implementation of Netflix's Archaius (https://github.com/Netflix/archaius/wiki/Users-Guide) hidden under Spring'sPropertySource. Archaius provides some great support for property change listeners, reloading properties, etc. that we use pretty heavily at Netflix, but being a Spring person I greatly prefer being able to use@Value, Environment, spel property resolution, and@ConfigurationPropertieswhenever possible. Hence theNetflixPropertySourceimplementation. Of course@Valuedoesn't work since its only reading the property once on startup/bean creation, and I don't think I'd want that to change honestly, but with@ConfigurationPropertieswould be much easier to implement delegation to the Spring Environment. I'd argue it should always delegate to thePropertySources, and if you're concerned about performance/caching let that be the concern of yourPropertySourceimplementation.As to whether or not reloadable configuration is a good idea in a world of micro cloud services, I'd just point out that this new cloud world isn't quite perfect yet. For us, rolling out new clusters into AWS (with sizes ranging from dozens to 100+ per app cluster) can take 45minutes just to get started and perhaps hours for caches to become warm. Because of that we often find it necessary to use feature flags based on properties to enable/disable functionality across our entire cloud infrastructure. This is something Archaius does rather nicely, and its just a shame at the moment I can't leverage
@ConfigurationPropertiesas much as I'd like to for working with Archaius.Any kind of hook to allow me to customize the creation of
@ConfigurationPropertieswould be sufficient, but I do suggest considering delegation to the Spring Environment as an option, if not the default behavior.andrewharmellaw commentedon Sep 9, 2014
Hi,
We're very interested in this discussion, and the recent announcement about dynamic Spring props. We've built https://github.com/Capgemini/archaius-spring-adapter which sounds like it overlaps heavily with what @twicksell has been working on. We'd be interested in being involved in work in this area as it'd help us greatly on our current project.
dsyer commentedon Sep 9, 2014
@andrewharmellaw our archaius bridge is here: https://github.com/spring-cloud/spring-cloud-netflix/tree/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/archaius. It would be good to get feedback, or pull requests if you like, via that project.
andrewharmellaw commentedon Sep 9, 2014
Cheers @dsyer We'll take a look and see if we can contribute. We're big users of Spring (Boot especially), Camel, and various Netflix bits and pieces too. Anything which helps glue them together which is OSS is of interest to us.
rachelwilson commentedon Jan 6, 2015
I was wondering if someone could point me (and other readers) in the direction of the "recent announcement about dynamic Spring props" as mentioned by andrewharmellaw above. I was interested to hear what the latest thinking is and I cant seem to find anything recent.
dsyer commentedon Jan 6, 2015
Try here: http://projects.spring.io/spring-cloud/spring-cloud.html#_environment_changes (and some other bits of that section).
rachelwilson commentedon Jan 7, 2015
Hmm, that seems to be documentation rather than an announcement as such. Can I assume in that case that the dynamic properties apply only to the Cloud and Boot products and there are no announced changes to the original Spring framework?
dsyer commentedon Jan 7, 2015
Yes, it's a Cloud feature right now (but with really minimal dependencies, as long as you are using Spring Boot).
nmarasoiu commentedon May 19, 2016
@dsyer Hi, I am having this issue with Spring Cloud: I added a MapPropertySource to the env, I am changing the values from the ConcurentMap from another thread, but I can't see those values reflecting in env.getProperty or in @ConfigurationProperties beans. Pls advise!
dsyer commentedon May 19, 2016
If you really changed the environment, you would see it in
getProperty(), so if you didn't see it there, then I suspect you didn't change it, or you changed a property that is overridden in another property sourec. You can see all the property sources in the /env endpoint.You have to rebind to get
@ConfigurationPropertiesto update, so that's a different problem. There's aContextRefresherin spring-cloud-context that you can use if you want to trigger it manually, or you can use the /refresh endpoint.9 remaining items
wilkinsona commentedon Jan 17, 2019
We've discussed the possibility of supporting a property that's a
Supplier<?>that retrieves the value from theEnvironmenteach timeget()is called. Our feeling is that this isn't a viable solution for two reasons:Our feeling, as it was above, is that an event-based approach would be better. The event would provide a complete new instance of a configuration properties class with the changed value or values. This addresses both of the problems described above while also working with immutable configuration properties that are currently in plan for 2.2.
The question remains if this is a good idea. We'd also be interested in hearing some of the use cases that a change in this area would enable. As things stand, there hasn't been particularly broad interest in this functionality. Given the reasonably large amount of complexity that it would introduce, it's difficult to justify implementing it at this time.
[-]@ConfigurationProperties should have an option to have properties read each time from Evironment [/-][+]@ConfigurationProperties should have an option to have properties read each time from Environment [/+]Code2Life commentedon Jan 22, 2022
Hi @philwebb @dsyer @wilkinsona,
I think the original feature this issue requests have been implemented in my project: https://github.com/Code2Life/spring-boot-dynamic-config
Adding
@DynamicConfigon@ConfigurationPropertiessimply works !With Spring Boot native features leveraged, and with the file watcher and hot-reload mechanism implemented by only 400+ lines of code,
@Value@ConfigurationPropertiescould always be the newest value.This lib is widely used in 20+ core applications for more than half a year in the company I'm working for. Hope this feature becomes a built-in feature of Spring Boot. @philwebb @dsyer @wilkinsona Would you like to take a look at the source codes and put some suggestions? Thanks !
denon82 commentedon Jan 23, 2022
Code2Life commentedon Jan 24, 2022
@denon82 Thanks !
philwebb commentedon Jan 24, 2022
Thanks very much for sharing the project @Code2Life. It looks like an interesting alternative to
@RefreshScopefor those that want it. I don't have any specific comments on the code (although I agree with @denon82 about Lombok). We're unlikely to get to this feature in Spring Boot itself for a little while so it's great that users have another option.Code2Life commentedon Jan 26, 2022
Thanks for reviewing @philwebb ,
@RefreshScoperelies on spring cloud stack and additional config server. we've tried spring-cloud-config and other spring-cloud components in production years ago and found it's too heavyweight, then we went back to Spring Boot.dsyer commentedon Jan 26, 2022
FYI
spring-cloud-configisn't needed for@RefreshScope, and also@RefreshScopeis not needed for@ConfigurationProperties. Just putspring-cloud-commonson the classpath - it's very lightweight, really - the only dependency is Spring Boot.shiyiyue1102 commentedon Mar 14, 2025
@RefreshScope may not be secure enough as it will first destroy the current bean and then create a new bean. What if the environment properties encounter an exception when creating a new bean? This online running application will be a disaster. Is it possible to adjust the order and first try creating a temporary bean? If it can be initialized normally, then replace the original bean. This will be a significant optimization for runtime configuration updates.
shiyiyue1102 commentedon Mar 14, 2025
In addition,if it is possible to move @RefreshScope from spring-cloud to spring boot? it should not be bundled to spring cloud project.
I am a core builder in the Nacos community (https://nacos.io/en/) which is widely used by chinese developer community and I am responsible for config center.
We want to implement all configurations on the hosted application side, support elegant updates during runtime, and improve the efficiency of business software iteration. If Spring can support elegant refresh beans, then there is no need for every second party middleware to implement this feature
wilkinsona commentedon Mar 14, 2025
There are no plans to move
@RefreshScopeto Spring Boot. If you have concerns about its behavior, please raise a Spring Cloud Commons issue.