Monday 7 November 2016

Java Source World: Life Cycle of a Spring Bean

Life Cycle of a Spring Bean | Java Source World


                                                                          Java Source World       A Java quick reference blog


Life Cycle of a Spring Bean:





 Spring bean life cycle





Description:



Now we will try to understand spring bean life cycle step step as per above diagram.




1. The bean container finds the definition of the spring bean in the configuration file, and then container creates an instance of the bean using java Reflection API.


2. If any properties are mentioned, those are also will be set, if the property itself is a bean, then it is resolved and set.


3. If the bean class implements the BeanNameAware interface, then the setBeanName() method will be called by passing the bean’s Id or the name.


4. If the bean class implements the BeanClassLoaderAware interface, then the method setBeanClassLoader() method will be called by passing an instance of the classloader object that loaded this bean.


5.  If the bean class implements the BeanFactoryName interface, then the method setBeanFactory will be called by passing an instance of BeanFactory object.


6. If there are any BeanPostProcessors object associated with the BeanFactory that loaded the Bean then the method postProcessBeforeInitialization() will be called even before the properties for the bean are set.


7. If the bean class implements the InitializingBean interface, then the method afterPropertiesSet() will be called once all the bean properties defined in the configuration file are set.


8. If the bean definition in the configuration file contains a ‘init-method’ attribute, then the value of the attribute will be resolved to a method name in the bean class and that method will be called


9. The postProcessAfterInitialization() method will be called if there are any bean post process attached for the bean factory object.


10. If the bean implements the DispossableBean interface then the method destroy() will be called when the application no longer needs the bean reference.


11. If the bean definition in the configuration file contains a ‘destroy-method’ attribute, then the corresponding method definition in the bean class will be called.



The Aware interface is a common pattern used within the spring framework. They are typically used to allow spring managed beans to be given an object (via the interfaces ‘set’ methods) at spring bootstrapping time.


During spring boot strapping, spring will examine each bean to determine if it implements any of the xxxAware interfaces. When one is found, it invokes the interface methods, providing the piece of information that is being asked for.

BeanClassLoaderAware:


Call back that allows a bean to be aware of the bean calss-loader; that is, the class loader used by the present bean factory to load bean classes.


BeanPostProcessor Interface:

           
This is to be used to have configuration related code before initialization (or) after initialization of beans.

It has the fallowing methods to do the above task:

postProcessBeforeInitialization(Object obj, String beanName)   
           
            postProcessAfterInitialization(Object obj, String beanName)



Continue reading:



                                                                  Java Source World       A Java quick reference blog








No comments:

Post a Comment