Spring: how to do transparent runtime changeable property configuration

configurationpropertiesspring

Spring has the nice mechanism PropertyPlaceholderConfigurer for injecting values like timeouts, JDBC Urls and so forth into Spring beans for configuration purposes. Is there a sensible way to handle configuration values that can change at runtime?

UPDATE: With Spring 3.1 there is a nice way to include non-static configuration sources such as the database via PropertySources. Some ApplicationContexts provide a refresh mechanism that is in principle able to handle changing configuration values. However it stops the application first, then creates all beans fresh and then starts the application context again. However, for our purposes I would need a way to do this transparently, such that the server correctly handles currently running requests.

Another idea to do this would be a custom Scope that creates fresh objects when the configuration changes. Unfortunately the ObjectFactory provided to the Scope uses a preprocessed bean definition, such that the placeholders are not read anew from the configuration. Thus the created objects have the same configuration. 🙁

Best Solution

Unfortunately configuration from properties files is static and happens at startup. What I am typically doing is exposing dynamic attributes via :

@ManagedResource
@Service
public class BusinessService {

    @ManagedAttribute
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void businessMethod() {
        //use age...
    }

}

Remember to add:

<context:mbean-export/>

To your configuration. Now you can access and change that attribute via jconsole or any other JMX client. See also: 23.3.2 Using Source-Level Metadata (JDK 5.0 annotations).