5

I have a property file and using Spring property place holder, I set values to the Spring beans. Now, this property file may be modified during the run time. Is there a way to refresh the properties of the Spring beans with this newly modified property value? Especially, I have many singleton beans? How can I refresh them with the new values? Is there already a solution to this or should it be custom coded? If it doesn't already exist, can someone please give the best approach to achieve this? Thanks!

PS: My application is a batch application. I use Spring based Quartz configuration to schedule the batches.

SJoe
  • 319
  • 2
  • 6
  • 14

1 Answers1

3

I'll leave this in for reference, but the updated answer is below the divider:

Well the ConfigurableApplicationContext interface contains a refresh() method, which should be what you want, but the question is: how to access that method. Whichever way you do it, you'll start with a bean that has a dependency of type ConfigurableApplicationContext:

private ConfigurableApplicationContext context;
@Autowired
public void setContext(ConfigurableApplicationContext ctx){
    this.context = ctx;
}

Now the two basic options I'd suggest would be to either

  1. use the Task Execution Framework and let your bean watch the property resources regularly, refreshing the ApplicationContext when it finds changes or
  2. expose the bean to JMX, allowing you to manually trigger the refresh.

Referring to comments: since it seems impossible to refresh the entire context, an alternative strategy would be to create a properties factory bean and inject that into all other beans.

public class PropertiesFactoryBean implements FactoryBean<Properties>{

    public void setPropertiesResource(Resource propertiesResource){
        this.propertiesResource = propertiesResource;
    }

    private Properties value=null;
    long lastChange = -1L;

    private Resource propertiesResource;

    @Override
    public Properties getObject() throws Exception{
        synchronized(this){
            long resourceModification = propertiesResource.lastModified();
            if(resourceModification != lastChange){
                Properties newProps = new Properties();
                InputStream is = propertiesResource.getInputStream();
                try{
                    newProps.load(is);
                } catch(IOException e){
                    throw e;
                } finally{
                    IOUtils.closeQuietly(is);
                }
                value=newProps;
                lastChange= resourceModification;
            }
        }
        // you might want to return a defensive copy here
        return value;
    }

    @Override
    public Class<?> getObjectType(){
        return Properties.class;
    }

    @Override
    public boolean isSingleton(){
        return false;
    }

}

You could inject this properties bean into all your other beans, however, you would have to be careful to always use prototype scope. This is particularly tricky inside singleton beans, a solution can be found here.

If you don't want to inject lookup methods all over the place, you could also inject a PropertyProvider bean like this:

public class PropertiesProvider implements ApplicationContextAware{

    private String propertyBeanName;
    private ApplicationContext applicationContext;

    public void setPropertyBeanName(final String propertyBeanName){
        this.propertyBeanName = propertyBeanName;
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException{
        this.applicationContext = applicationContext;
    }

    public String getProperty(final String propertyName){
        return ((Properties) applicationContext.getBean(propertyBeanName)).getProperty(propertyName);
    }

}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Wouldn't this load the entire application context? Can you please share some sample? I am pretty new to Spring so I may be wrong. I also thought about reloading the entire spring application context, but that would mean, other currently running processes could be disrupted. Or maybe I am thinking wrong! – SJoe Nov 03 '10 at 08:50
  • In that case wouldn't my currently running process get disrupted. Mine is a batch application and it could take hours to complete a process. Additionally, I have a configured SchedularFactoryBean through Spring. My question is, if I reload the Application Context fully, wouldn't the scheduler be reset? – SJoe Nov 03 '10 at 09:07
  • Probably. Maybe you should have said in your question that this is a batch app. – Sean Patrick Floyd Nov 03 '10 at 09:11
  • Thanks! I've added that to the question. – SJoe Nov 03 '10 at 09:13
  • Thanks! I think this is going to work for me. I am going to try it out. Much appreciated! – SJoe Nov 03 '10 at 10:32
  • Can you also include new jar files in the project with new classes and write the application context again with new class definition then call this refresh? will it work then? – Monis Iqbal Jan 11 '12 at 13:26
  • @MonisIqbal I doubt it, but you should ask this as a separate question – Sean Patrick Floyd Jan 11 '12 at 14:26