Best view in Firefox and Chrome

Dynamic Bean creation in Spring

Monday, October 26, 2009
Convert Article to PDFPrint ArticleEmail Article to FrinedBookmark this Article



Normal we are defining our Spring beans in the Application context(XML) file and we will register those beans to the Bean's container during the start up time or even after the start up time of an application.. This approach is actually very straight forward, easy and flexible to register and use those beans. But sometimes we need to create beans programmable. During run time of an application we need to create and register some beans in the container. Will it possible with the Spring framework. Yeah! the Spring is supporting for this dynamic bean creation mechanism.

Normally Spring provides two most fundamental and important packages org.springframework.beans and org.springframework.context which provides the basis for Inversion of Control features to the Spring framework. The BeanFactory provides an advanced configuration mechanism capable of managing beans (objects) of any nature, using potentially any kind of storage facility. The ApplicationContext builds on top of the BeanFactory and adds other functionality to the factory. GenericApplicationContext which implements the BeanDefinitionRegistry interface in order to allow for applying any bean definition readers to it. Then we register a variety of bean definitions via the BeanDefinitionRegistry interface and then call AbstractApplicationContext.refresh() to initialize those beans with application context semantics (handling ApplicationContextAware, auto-detecting BeanFactoryPostProcessors, etc). Now we see the codes. This can be use in your porgram it self.

GenericApplicationContext ctx = new GenericApplicationContext();
BeanDefinitionBuilder bDBuilder = BeanDefinitionBuilder .rootBeanDefinition(CLASS_NAME.class);
bDBuilder.addPropertyValue("PROPERTY_NAME", PROPERTY_VALUE);
ctx.registerBeanDefinition("NAME_OF_THE_BEAN", bDBuilder.getBeanDefinition());
CLASS_NAME objectRef = (CLASS_NAME)ctx.getBean(containerName);
 


Enjoy..!


Read these Articles :

0 comments:

Why Breeze

This is a new technological rhythm in the web. It blooms like a Breeze in your technological face with more than 75 technoligical atricles and guidences.

Contact Me
Contact Me Send an Email Facebook Account LinkedIn profile Twitter/Shayanth
Share And Save
Social Share with Twitter Facebook Share Add to Google Save on Delicious
Get Updates

The RSS will facilitate you to get the updates from Breeze. Click Here to redirect to the RSS link.

Designed by Posicionamiento Web | Bloggerized by GosuBlogger | Blue Business Blogger | Customized by SHAUOM