Loading and Removing bean at run time in Spring Application

26 / Jul / 2015 by Anil Agarwal 0 comments

Hi friends,

Sometimes, we face some issues while configuring beans in Spring application. We may have to restart the application each time we made any changes in configuration file. To avoid such problems, we have an option to load the beans at run time

ConfigurableApplicationContext configContext = (ConfigurableApplicationContext)applicationContext;
SingletonBeanRegistry beanRegistry = configContext.getBeanFactory();
MyBean bean=new MyBean();
beanRegistry.registerSingleton("myBeanName", bean);

And if we need to remove registered beans at runtime, we can do the same as below

beanRegistry.removeBeanDefinition("bean") 
                  or
beanRegistry.destroySingleton("bean")

Cheers!!!

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *