Friday, May 25, 2012

Spring Framework- Introducing Bean and Bean’s life(BeanFactory).



In traditional Java application the life cycle of bean is very simple-
Java’s new keyword is used to instantiate the bean and it ready to use.
Once the bean is create it no longer to use,it is also eligible for garbage collection.
In Spring Framework  contain extra functionality. Here it is important to understand the life cycle of the bean. Because to take the advantage of opportunities that the spring provided related to the bean or bean life cycle. The spring handle beans in two different ways-
      1.  Bean Factory.
      2.   ApplicationContext.
Here first discuss that  what several setup steps performs by the beanfacory before beans is ready to use.

                                                       
  1.   Instantiate – First Spring instantiate the bean.
  2.  Populate properties- Spring Inject the bean’s properties.
  3.  Set Bean Name- Spring set bean name. if the bean implements BeanNameAware, spring passes .The  bean’s id to setBeanName() method.
  4. Set Bean factory-If Bean implements BeanFactoryAware ,spring passes the beanfactory to          setBeanFactory().
  5. Pre Initialization-  It also called postprocess of bean . if there are any bean BeanPostProcessors,  Spring calls postProcesserBeforeInitialization () method.
  6. Initialize beans-   If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is  Called.
  7. Post Initialization-  If there is BeanPostProcessors, is implements , spring calls their postProcessAfterinitalization() method.
  8.  Ready to use- Now the bean is ready to use by the application.
  9. Destroy- If the bean implement DisposableBean , it will call the destroy() method . If custom  destroy () method is defined . the specified method is called.


For Further Reading,
General, Java, spring

2 comments:

Rajinder on August 20, 2012 at 8:25 PM said...

simple and nice post!!!

WebAppicationContext and XmlWebApplicationContext(Application Contexts used in a Web Applications)

Unknown on March 11, 2013 at 1:12 AM said...

thanks rajinder

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.