1

With spring cloud config, when I update a configuration and call refresh on any clients, is there a way that I can have a notification that this happened? If I am constructing objects based on some @ConfigurationProperties, I will want to refresh these objects with the new state of those hierarchical properties. I would rather not perform lookups each time I need to reference the config props; in my case it is best to refresh certain objects at the time of config changes. So, is there a way to hook into that refresh lifecycle?

Edit: Ideally, if I could have a @Configuration class know about the refresh event, and re-bind/re-instantiate some relevant Spring @Beans, that would be quite ideal!

Steve Storck
  • 793
  • 6
  • 25

1 Answers1

2

Ok, so when I posted this question a month ago, I was obviously pretty new to this, and pretty naive. I have since learned that @ConfigurationProperties get updated when spring cloud config clients are refreshed. Say that you have a bean (lombok to reduce boilerplate, of course):

@Data
public class ClientSettings {
    private List<String> list1 = new ArrayList<>();
    private List<String> list2 = new ArrayList<>();
    private List<String> list3 = new ArrayList<>();
}

And you have a @Configuration class like this:

@Configuration
public class PropsConfig {

    @Bean
    @RefreshScope
    @ConfigurationProperties(prefix = "settings")
    public ClientSettings clientSettings() {
        return new ClientSettings();
    }
}

If you update the config file that the spring cloud config server is serving, and then call the refresh actuator endpoint on the client, the underlying bean will be updated, and any services that have this bean wired will have an updated bean after it was refreshed.

So, bravo to Spring for implementing this magic voodoo wizardry so well! All kidding aside, the autowired bean is proxied, so it makes sense that if the bean registry is updated with new values, that the services with the injected singleton will have the updated values. That brings up the question of what would happen if the same @Configuration @Bean method added @Scope(SCOPE_PROTOTYPE). Since @RefreshScope is a specialized scope for spring cloud config, and since the annotation is not repeatable, I am not sure what would happen.

Steve Storck
  • 793
  • 6
  • 25