Thursday, September 21, 2017

Spring Life Cycle

Bean Life Cycle

1. The Bean Container finds the definition of the Spring Bean in the Configuration file.

2. The Bean Container creates an instance of the Bean using Java Reflection API.

3. If any properties are mentioned, then they are also applied. If the property itself is a Bean, then it is resolved and set.

4. If the Bean class implements the BeanNameAware interface, then the setBeanName() method will be called by passing the name of the Bean.

5. 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.
 
6. If the Bean class implements the BeanFactoryAware interface, then the method setBeanFactory() will be called by passing an instance of
BeanFactory  object.

7. 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.

8. 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.
 
9. If the Bean definition in the Configuration file contains a 'init-method' attribute, then the value for the attribute will be resolved to a method
   name in the Bean class and that method will be called.
 
10.The postProcessAfterInitialization() method will be called if there are any Bean Post Processors attached for the Bean Factory object.

11. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the
    bean reference.
 
12. 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.


Spring framework provides following 4 ways for controlling life cycle events of bean:


1. InitializingBean and DisposableBean callback interfaces=>
InitalizingBean => void afterPropertiesSet() throws Exception;
DisposableBean =>  void destroy() throws Exception;

2. Other Aware interfaces for specific behavior=> 
BeanFactoryAware=>void setBeanFactory(BeanFactory beanFactory) throws BeansException
BeanNameAware =>void setBeanName(String name);
ApplicationContextAware =>void setApplicationContext(ApplicationContext applicationContext) throws BeansException


3. custom init() and destroy() methods in bean configuration file


4. @PostConstruct and @PreDestroy annotations


Spring BeanPost Processors

BeanPostProcessor Interface=>
postProcessBeforeInitialization()=> This method gets called before initialization
postProcessAfterInitialization ()=> This method gets called after the object is initialized


EmployeeBeanProcessor .java


/**
 *
 */
package com.bebo.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;


/**
 * @author anthakur
 *
 */
@Component
public class EmployeeBeanProcessor implements BeanPostProcessor {

/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessAfterInitialization(Object bean, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("**********postProcessAfterInitialization**************"+bean);
return bean;
}

/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("************postProcessBeforeInitialization************"+bean);
return bean;
}

}


EmployeeUtility  .java


/**
 *
 */
package com.bebo.component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author anthakur
 *
 */
@Component
public class EmployeeUtility  implements InitializingBean, DisposableBean,BeanFactoryAware, BeanNameAware, ApplicationContextAware{


public void display() {
System.out.println("EmployeeUtility");
}

/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("EmployeeUtility#afterPropertiesSet is invoked.....");

}

/* (non-Javadoc)
* @see org.springframework.beans.factory.DisposableBean#destroy()
*/
@Override
public void destroy() throws Exception {
System.out.println("EmployeeUtility#destroy is invoked.....");

}


/* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("EmployeeUtility#setApplicationContext  "+arg0);
}

/* (non-Javadoc)
* @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
*/
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("EmployeeUtility#setBeanName  "+arg0);

}

/* (non-Javadoc)
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
*/
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("EmployeeUtility#setBeanFactory  "+arg0);

}

@PostConstruct
public void init(){
System.out.println("EmployeeUtility#init  ");

}

@PreDestroy
public void destroytext(){
System.out.println("EmployeeUtility#destroy  ");

}


}







No comments:

Post a Comment