Wednesday, April 27, 2016

Spring Bean Life Cycle

Spring Bean Life Cycle

Spring beans are managed by Spring IOC container. Various types of life cycle interfaces will be involved during bean life cycle by this container.

If any bean want to be used by application some initialization process needs to be followed and the same way for destroying some destroying process needs to follow. Spring container following some process and this process we are calling as Bean Life Cycle.

Flow Diagram Overview

Bean
Spring Bean Life Cycle Diagram

Order of Execution:

1. IOC container look for the spring bean definition in the configuration file.
2. Once find container will create instance of the bean by using Java Reflection API.
3. If any properties mentioned, it will be populated and Dependency will be injected.
4. If the bean implements InitializingBean interface then it should implements its method
 void afterPropertiesSet() throws Exception;
5. If the bean implements DisposbleBean interface then it should implements its method
void destroy() throws Exception;

The above process is looks like simple, but mostly its not recommended since it will create tight coupling.
6. If the bean implements any of the following Aware interfaces for the specific behavior, then that process will be started with its methods. See, if you are implements any of the following, then only it will be executed. 

1. ApplicationContextAware
2. ApplicationEventPublisherAware
3. BeanClassLoaderAware
4. BeanFactoryAware
5. BeanNameAware
6. LoadTimeWeaverAware
7. MessageSourceAware
8. NotificationPublisherAware
9. ResourceLoaderAware

Below sample code implementing all of above interfaces and overriding its methods.
public class TestBean implements ApplicationContextAware,
        ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware,
        BeanNameAware, LoadTimeWeaverAware, MessageSourceAware,
        NotificationPublisherAware, ResourceLoaderAware
{
    @Override
    public void setResourceLoader(ResourceLoader arg0) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setNotificationPublisher(NotificationPublisher arg0) {
        // TODO Auto-generated method stub
 
    }
 
    @Override
    public void setMessageSource(MessageSource arg0) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setLoadTimeWeaver(LoadTimeWeaver arg0) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setBeanName(String arg0) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setBeanClassLoader(ClassLoader arg0) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher arg0) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        // TODO Auto-generated method stub
    }
}


7. Suppose in your configuration file, bean uses init-method attributes , then that method will be called. Sample configuration code below. 




    
  

8. Same way bean configuration uses default-destroy-method attributes , then that method will be called . Above sample configuration code have that.

Below is the simple custom init and destroy method.
public class TestBean 
{
    public void customInitmethod() 
    {
        System.out.println("Custom Init() invoked");
    }
 
    public void customDestroymethod() 
    {
        System.out.println("CustomDestroy() invoked");
    }
}

No comments:

Post a Comment