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-
Here first discuss that what several setup steps performs by the beanfacory before beans is ready to use.
- Populate properties- Spring Inject the bean’s properties.
- Set Bean Name- Spring set bean name. if the bean implements BeanNameAware, spring passes .The bean’s id to setBeanName() method.
- Set Bean factory-If Bean implements BeanFactoryAware ,spring passes the beanfactory to setBeanFactory().
- Pre Initialization- It also called postprocess of bean . if there are any bean BeanPostProcessors, Spring calls postProcesserBeforeInitialization () method.
- 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.
- Post Initialization- If there is BeanPostProcessors, is implements , spring calls their postProcessAfterinitalization() method.
- Ready to use- Now the bean is ready to use by the application.
- 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,
2 comments:
simple and nice post!!!
WebAppicationContext and XmlWebApplicationContext(Application Contexts used in a Web Applications)
thanks rajinder
Post a Comment