xtian
xtian

Reputation: 3169

Reloading a Commons Configuration 2 Spring bean

I've been using the reload feature of Commons Configuration v1 without any extra coding when exposing a Configuration object as a Spring @Bean, because the reload check was performed every time the Configuration was accessed.

I'm now trying to migrate to Commons Configuration v2 and I read that reload is only effective on new Configuration objects created by the builder.

In other words, while in v1 I could do something like

@Bean
public Configuration config() {
    ...
    return builder.getConfiguration();
}

then inject the configuration with

@Autowired Configuration config;

and expect it to reload (when needed) on a

config.getString("somepath");

now I should call

builder.getConfiguration()

again each time I want a fresh configuration.

So how do I go about it? Can anything in Spring help me "refresh" a @Bean that has already been injected in many @Controllers? It doesn't have to be automatic: I could implement a "reload" button in the admin console to trigger it.

Upvotes: 0

Views: 773

Answers (1)

xtian
xtian

Reputation: 3169

Maybe I just have to wrap the Configuration in a MyConfiguration class, exposed as a @Bean, with a method that rebuilds the configuration, to be called from a console. Something like:

public class MyConfiguration {
    private Configuration configuration;

    ... inject builder somehow here

    public void reload() {
        builder.getReloadingController().checkForReloading(null);
        configuration = builder.getConfiguration();
    }

    public String getString(String key) {
        return configuration.getString(key);
    }

    ... all other delegated methods follow

}

Upvotes: 0

Related Questions