A step by step analysis of spring bean life cycle

 About spring bean life cycle is the basis for in-depth study of the spring, but also difficult, this article will use the code + graphic way to explain the conclusions of spring bean life cycle,

This article will explain clearly the figure below.

 

 A project structure and source code

1. Directory Structure

 2.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.demo.dao.UserDao" id="userDao" scope="singleton" init-method="myInit" destroy-method="myDestroy">
        <property name="userName" value="Alan_beijing"/>
    </bean>

    <bean class="com.demo.dao.MyBeanPostProcessor" id="myBeanPostProcessor"/>

</beans>

3.UserDao.java

package com.demo.dao;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.apache.log4j.Logger;

public class UserDao implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean,
        DisposableBean{

    private String userName;
    private int count = 0;

    public String, getUserName () {
         return the userName; 
    } 

    // 2. Properties injection, injection property is the userName 
    public  void setUserName (String the userName) { 
        COUNT ++ ; 
        System.out.println (COUNT + ": = injecting property the userName" + the userName );
         the this .userName = the userName; 
    } 

    // 1. constructor with no arguments for calling the constructor when instantiating 
    public UserDao () { 
        COUNT ++ ; 
        System.out.println (COUNT + ": UserDao calling the constructor () " ); 
    } 

    // 3. implemented BeanNameAware, ID obtaining the bean 
    public  voidsetBeanName (String S) { 
        COUNT ++ ; 
        System.out.println (COUNT + ": call setBeanName () Gets ID bean, bean ID =" + S); 
    } 

    // 4. implemented BeanFactoryAware, obtaining bean factory 
    public  void setBeanFactory (the beanFactory beanFactory) throws BeansException { 
        COUNT ++ ; 
        System.out.println (COUNT + ": setBeanFactory call () Gets bean plants, beanFactory =" + beanFactory); 
    } 

    // 5. The implement ApplicationContextAware, bean context obtaining 
    public  void setApplicationContext (ApplicationContext applicationContext) throws  BeansException {
        COUNT++ ; 
        System.out.println (COUNT + ": call setApplicationContext () Gets bean context, applicationContext =" + applicationContext); 
    } 

    // 6. The implement InitializingBean, obtaining afterPropertiesSet 
    public  void afterPropertiesSet () throws Exception { 
        COUNT ++ ; 
        System.out.println (COUNT + ": call afterPropertiesSet ()" ); 
    } 

    // 7. The custom initialization method MyInit () 
    public  void MyInit () { 
        COUNT ++ ; 
        System.out.println (COUNT + ": call custom MyInit () " );
    } 

    //8. achieve DisposableBean, acquires the destroy () 
    public  void the destroy () throws Exception { 
        COUNT ++ ; 
        System.out.println (COUNT + ": the destroy ()" ); 
    } 

    // 9. The method of destruction custom myDestroy () 
    public  void myDestroy () { 
        COUNT ++ ; 
        System.out.println (COUNT + ": custom calls the destroy ()" ); 
    } 
}
4.MyBeanPostProcessor.java
package com.demo.dao;

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

public  class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("=====调用postProcessBeforeInitialization()=====");
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("=====调用postProcessAfterInitialization()=====");
        return bean;
    }
}

 Two test code and test results

1.test.java

package com.demo.test;

import com.demo.dao.UserDao;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test() {
        //定义容器并初始化
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        to applicationContext.getBean (UserDao. class );
         // be in the closed container, will destroy method is called 
        applicationContext.registerShutdownHook (); 
    } 
}

2. Test results

 Analysis of Three

With the above test results, bean lifecycle process is as follows:

 

 1. Assembly bean

bean assembly for the first part of the life cycle of bean. The so-called bean assembly, refers to the process of converting the bean java object. In this example, UserDao.jave and MyBeanPostProcessor xml manner into the bean.

Note: spring mounting frame supports four way bean: xml embodiment, java codes embodiment, automatic assembly and mixing assembly methods

2. Load and instantiate applicationContext.xml

Loaded and instantiated bean bean is the second part of the life cycle. Exemplified herein by the ClassPathXmlApplicationContext to load () and, when the bean is singleton, this process is instantiated objects, without having to wait

Call applicationContext.getBean () Gets bean when instantiating an object, which prototype is not the same.

3. Properties injection

injection bean properties for the bean lifecycle third aspect, reflective mode injection bean.

 

4. The realization BeanNameAware, obtaining bean id

The procedure is the fourth part of the life cycle of bean, implement the interface, you can get the bean id

 

5. Implement BeanFactoryAware, obtaining bean plant

The procedure is the fifth part of the life cycle of bean, bean plant acquired by implementing BeanFactoryAware

 6. Implement ApplicationContextAware, get use context

该过程为bean生命周期第六环节,通过实现ApplicationContextAware接口,获取bean上下文

 7.调用Bean后置处理器,before

该过程为bean生命周期第七环节,通过实现后置处理器BeanPostProcessor获取before和after,该过程是通过AOP方式实现的,在before和after之间,发生如下8,9过程。

8.实现InitializingBean的afterPropertiesSet(),获取初始化方法

该过程为bean生命周期第八环节,通过实现InitializingBean,获取afterPropertiesSet()

 9.调用自定义初始化方法,init-method

该过程为bean生命周期第九环节,实现自定义初始化方法

 10.调用Bean后置处理器after

该过程为bean生命周期第十环节,后置处理器最后环节

 11.关闭容器AbstractApplicationContext.registerShutDownHook()

该环节为bean生命周期第十一环节,关闭容器

 

 12.调用DisposableBean的destroy()

该过程为bean生命周期第十二环节,实现DisposableBean接口,调用destroy()

 13.调用定制化销毁方法destroy-method

该过程为bean生命周期最后环节,调用自定义销毁方法destroy-method

 三  版权区

  •   转载博客,必须注明博客出处
  •    博主网址:http://www.cnblogs.com/wangjiming/
  •    如您有新想法,欢迎提出,邮箱:[email protected]
  •   专业.NET之家技术QQ群:490539956
  •   专业化Java之家QQ群:924412846
  •   有问必答QQ群:2098469527
  •   一对一技术辅导QQ:2098469527

Guess you like

Origin www.cnblogs.com/wangjiming/p/11669091.html