{"id":23781,"date":"2015-07-26T14:23:25","date_gmt":"2015-07-26T08:53:25","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23781"},"modified":"2015-07-26T14:23:25","modified_gmt":"2015-07-26T08:53:25","slug":"loading-and-removing-bean-at-run-time-in-spring-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/loading-and-removing-bean-at-run-time-in-spring-application\/","title":{"rendered":"Loading and Removing bean at run time in Spring Application"},"content":{"rendered":"<p>Hi friends,<\/p>\n<p>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<\/p>\n<pre class=\"lang-java prettyprint prettyprinted\"><code><span class=\"typ\">ConfigurableApplicationContext<\/span><span class=\"pln\"> configContext <\/span><span class=\"pun\">=<\/span> <span class=\"pun\">(<\/span><span class=\"typ\">ConfigurableApplicationContext<\/span><span class=\"pun\">)<\/span><span class=\"pln\">applicationContext<\/span><span class=\"pun\">;<\/span>\r\n<span class=\"typ\">SingletonBeanRegistry<\/span><span class=\"pln\"> beanRegistry <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> configContext<\/span><span class=\"pun\">.<\/span><span class=\"pln\">getBeanFactory<\/span><span class=\"pun\">();\r\nMyBean bean=new MyBean();<\/span><span class=\"pln\">\r\nbeanRegistry<\/span><span class=\"pun\">.<\/span><span class=\"pln\">registerSingleton<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"myBeanName\"<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> bean<\/span><span class=\"pun\">);<\/span><\/code><\/pre>\n<p>And if we need to remove registered beans at runtime, we can do the same as below<\/p>\n<pre class=\"lang-java prettyprint prettyprinted\"><code>beanRegistry.removeBeanDefinition(\"bean\") \r\n                  or\r\nbeanRegistry.destroySingleton(\"bean\")\r\n<\/code><\/pre>\n<p>Cheers!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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(); [&hellip;]<\/p>\n","protected":false},"author":235,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":245},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23781"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/235"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23781"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}