Initialize spring beans without app server restart or at runtime

Is there a way to re-initialize the spring beans dynamically ?

On app startup I Initialize spring beans through ContextLoaderListener in web.xml.

My use case is that at runtime there could be a case where new property files were loaded into memory(via Apache commons configuration) and I want to reinitialize the beans so that this can take into affect without having to restart.

Any pointers on this is appreciated.


能够通过让类实现ApplicationContextAware来解决它

public class ReloadConfig implements ApplicationContextAware{

private static Logger log = Logger.getLogger(ReloadConfig.class);


private Config config;

@Autowired
ApplicationContext applicationContext;

private ReloadConfig() {
    // Exists only to defeat instantiation.
    config = Config.getInstance();
}

public void reloadIfNotLoaded() throws ConfigurationException{

    CompositeConfiguration configuration = new CompositeConfiguration();

    if(config.getHealthFile() == null){

        log.info("Reloading Adding default properties found in config.properties");
        configuration.addConfiguration(new PropertiesConfiguration("config.properties"));


        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext)getApplicationContext();
        configurableApplicationContext.refresh();
        setApplicationContext(configurableApplicationContext);
    }



}

public void setApplicationContext(ApplicationContext context) throws BeansException {
    applicationContext = context;
}

public ApplicationContext getApplicationContext() {
    return applicationContext;
}
链接地址: http://www.djcxy.com/p/82038.html

上一篇: 在bean创建中使用Spring contextloaderlistener角色

下一篇: 在没有应用程序服务器重启或运行时初始化spring bean