Spring Bean的生命周期

旧城冷巷雨未停 提交于 2020-04-06 03:45:25

容器开始启动:

  1. 如果Spring容器中注册了实现BeanFactoryPostProcessor接口的容器后处理器,实例化该容器后处理器,即调用构造函数创建实例
  2. 如果Spring容器中注册了实现BeanFactoryPostProcessor接口的容器后处理器,就调用BeanFactoryPostProcessor.postProcessBeanFactory()方法

容器运行中:

  1. 如果Spring容器中注册了实现InstantiationAwareBeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessBeforeInstantiation方法。
  2. 实例化bean,即调用Bean的构造函数
  3. 如果Spring容器中注册了实现InstantiationAwareBeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessAfterInstantiation方法。
  4. 如果Spring容器中注册了实现InstantiationAwareBeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessProperties方法。
  5. 如果Spring容器中注册了实现InstantiationAwareBeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessPropertyValues方法。
  6. 初始化bean实例的成员变量的值,依赖注入
  7. 如果目标bean实现了BeanNameAware接口,就会调用setBeanName()方法。
  8. 如果Bean实现了BeanClassLoaderAware接口,就会调用setBeanClassLoader()方法。
  9. 如果目标bean实现了BeanFactoryAware接口,就会调用setBeanFactory()方法。
  10. 如果目标bean实现了ApplicationContextAware接口,就会调用setApplicationContext()方法。
  11. 如果Spring容器中注册了实现BeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessBeforeInitialization方法。
  12. 如果Spring容器中注册了实现InstantiationAwareBeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessBeforeInitialization方法。
  13. 如果目标bean实现了InitializingBean接口,就执行bean实例的afterPropertiesSet方法。
  14. 如果目标bean在配置文件中配置init-method属性,就执行<bean>的init-method属性指定的初始化方法。
  15. 如果Spring容器中注册了实现BeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessAfterInitialization方法。
  16. 如果Spring容器中注册了实现InstantiationAwareBeanPostProcessor接口的Bean后处理器,就执行该Bean后处理器的postProcessAfterInitialization方法。

容器关闭后:

  1. 如果目标bean实现了DisposableBean接口,就执行bean实例的destroy()方法。
  2. 如果目标bean在配置文件中配置destroy-method属性,就执行<bean>的destroy-method属性指定的方法。

源码:

自定义bean

package com.beans.lifecycle.demo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
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;

public class SpringBean implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean, ApplicationContextAware {

    private String desc;
    
    private BeanFactory beanFactory;
    
    private String beanName;
    
    public SpringBean() {
        System.out.println("【TargetBean】调用构造函数SpringBean()实例化");
    }
    
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        System.out.println("【TargetBean】注入属性desc:"+desc);
        this.desc = desc;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("【TargetBean】调用BeanFactoryAware.setBeanFactory()");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String beanName) {
        System.out.println("【TargetBean】调用BeanNameAware.setBeanName():beanName="+beanName);
        this.beanName = beanName;
    }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("【TargetBean】调用ApplicationContextAware.setApplicationContext()");
    this.applicationContext = applicationContext;
  }

    @Override
    public void destroy() throws Exception {
        System.out.println("【TargetBean】调用DiposibleBean.destory()");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【TargetBean】调用InitializingBean.afterPropertiesSet()");
    }
    
    // 通过<bean>的init-method属性指定的初始化方法
    public void myInit() {
        System.out.println("【TargetBean】调用<bean>的init-method属性指定的初始化方法");
    }
    
    // 通过<bean>的destroy-method属性指定的初始化方法
    public void myDestory() {
        System.out.println("【TargetBean】调用<bean>的destroy-method属性指定的初始化方法");
    }
    
    @Override
    public String toString() {
        return "TargetBean.toString()=TargetBean[ desc="+desc+" ]";
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("【TargetBean】调用BeanClassLoaderAware.setBeanClassLoader():classLoader="+classLoader);
    }

}

 

实现BeanFactoryPostProcessor接口的容器后处理器
package com.beans.lifecycle.demo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public MyBeanFactoryPostProcessor() {
        System.out.println("【实现BeanFactoryPostProcessor接口的容器后处理器MyBeanFactoryPostProcessor】 调用构造函数实例化. ");
    }
    
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
        System.out.println("【实现BeanFactoryPostProcessor接口的容器后处理器MyBeanFactoryPostProcessor】 调用BeanFactoryPostProcessor.postProcessBeanFactory()方法");
    }

}

实现BeanFactoryPostProcessor,Ordered接口的容器后处理器

package com.beans.lifecycle.demo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;

public class MyBeanFactoryPostProcessorImplOrdered implements BeanFactoryPostProcessor,Ordered {
    
    private int index;
    
    public MyBeanFactoryPostProcessorImplOrdered() {
        System.out.println("【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器 "+toString()+"】 调用构造函数MyBeanFactoryPostProcessor()实例化 ");
    }
    
    public MyBeanFactoryPostProcessorImplOrdered(int index) {
        this.index = index;
        System.out.println("【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器"+toString()+"】 调用构造函数MyBeanFactoryPostProcessor(int index)实例化 ");
    }
    
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
        System.out.println("【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器 "+toString()+"】 调用BeanFactoryPostProcessor.postProcessBeanFactory()方法");
    }

    @Override
    public int getOrder() {
        return this.index;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return "MyBeanFactoryPostProcessorImplOrdered [index=" + index + "]";
    }

}

实现BeanPostProcessor接口的Bean后处理器

package com.beans.lifecycle.demo;

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

public class MyBeanPostProcessor implements BeanPostProcessor {

    
    public MyBeanPostProcessor() {
        super();
        System.out.println("【实现BeanPostProcessor接口的Bean后处理器】实例化");
    }
    
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("【实现BeanPostProcessor接口的Bean后处理器】调用BeanPostProcessor.postProcessBeforeInitialization()方法");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
    
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("【实现BeanPostProcessor接口的Bean后处理器】调用BeanPostProcessor.postProcessAfterInitialization()方法");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
    
}

实现InstantiationAwareBeanPostProcessor接口的Bean后处理器

package com.beans.lifecycle.demo;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
    
    public MyInstantiationAwareBeanPostProcessor() {
        super();
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】实例化");
    }
    
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation方法:beanName="+beanName);
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }
    
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation方法:beanName="+beanName);
        return super.postProcessAfterInstantiation(bean, beanName);
    }
    
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization方法:beanName="+beanName);
        return super.postProcessBeforeInitialization(bean, beanName);
    }
    
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessAfterInitialization方法:beanName="+beanName);
        return super.postProcessAfterInitialization(bean, beanName);
    }
    
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
            throws BeansException {
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessProperties方法:beanName="+beanName);
        return super.postProcessProperties(pvs, bean, beanName);
    }
    
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
            String beanName) throws BeansException {
        System.out.println("【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessPropertyValues方法:beanName="+beanName);
        return super.postProcessPropertyValues(pvs, pds, bean, beanName);
    }

}

spring配置

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://www.springframework.org/schema/beans"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!-- 配置Bean后处理器,无须指定id属性 -->
    <bean class="com.beans.lifecycle.demo.MyBeanPostProcessor" />
    <bean class="com.beans.lifecycle.demo.MyInstantiationAwareBeanPostProcessor" />
    <!-- 配置容器后处理器,无须指定id属性 -->
    <bean class="com.beans.lifecycle.demo.MyBeanFactoryPostProcessor" />
    <bean class="com.beans.lifecycle.demo.MyBeanFactoryPostProcessorImplOrdered">
        <constructor-arg name="index" value="1"></constructor-arg>
    </bean>
    <bean class="com.beans.lifecycle.demo.MyBeanFactoryPostProcessorImplOrdered">
        <constructor-arg name="index" value="2"></constructor-arg>
    </bean>
    <bean class="com.beans.lifecycle.demo.MyBeanFactoryPostProcessorImplOrdered">
        <constructor-arg name="index" value="3"></constructor-arg>
    </bean>
    
    <bean id="springBean" class="com.beans.lifecycle.demo.SpringBean" init-method="myInit" destroy-method="myDestory">
        <property name="desc" value="descValue" />  
    </bean>
</beans> 

测试类

package com.beans.lifecycle.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Ctest01 {

    public static void main(String[] args) {
        System.out.println("---------------------现在开始初始化容器---------------------");
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("xmlBeans.xml");
        System.out.println("---------------------容器初始化成功---------------------");
        SpringBean targetBean = applicationContext.getBean("springBean", SpringBean.class);
        System.out.println(targetBean.toString());
        System.out.println("---------------------现在开始关闭容器!---------------------");
        applicationContext.registerShutdownHook();
    }

}

执行结果如下:

---------------------现在开始初始化容器---------------------
【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器MyBeanFactoryPostProcessorImplOrdered [index=1]】 调用构造函数MyBeanFactoryPostProcessor(int index)实例化 
【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器MyBeanFactoryPostProcessorImplOrdered [index=2]】 调用构造函数MyBeanFactoryPostProcessor(int index)实例化 
【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器MyBeanFactoryPostProcessorImplOrdered [index=3]】 调用构造函数MyBeanFactoryPostProcessor(int index)实例化 
【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器 MyBeanFactoryPostProcessorImplOrdered [index=1]】 调用BeanFactoryPostProcessor.postProcessBeanFactory()方法
【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器 MyBeanFactoryPostProcessorImplOrdered [index=2]】 调用BeanFactoryPostProcessor.postProcessBeanFactory()方法
【实现BeanFactoryPostProcessor,Ordered接口的容器后处理器 MyBeanFactoryPostProcessorImplOrdered [index=3]】 调用BeanFactoryPostProcessor.postProcessBeanFactory()方法
【实现BeanFactoryPostProcessor接口的容器后处理器MyBeanFactoryPostProcessor】 调用构造函数实例化. 
【实现BeanFactoryPostProcessor接口的容器后处理器MyBeanFactoryPostProcessor】 调用BeanFactoryPostProcessor.postProcessBeanFactory()方法
【实现BeanPostProcessor接口的Bean后处理器】实例化
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】实例化
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation方法:beanName=springBean
【TargetBean】调用构造函数SpringBean()实例化
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation方法:beanName=springBean
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessProperties方法:beanName=springBean
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessPropertyValues方法:beanName=springBean
【TargetBean】注入属性desc:descValue
【TargetBean】调用BeanNameAware.setBeanName():beanName=springBean
【TargetBean】调用BeanClassLoaderAware.setBeanClassLoader():classLoader=sun.misc.Launcher$AppClassLoader@404b9385
【TargetBean】调用BeanFactoryAware.setBeanFactory()【TargetBean】调用ApplicationContextAware.setApplicationContext()
【实现BeanPostProcessor接口的Bean后处理器】调用BeanPostProcessor.postProcessBeforeInitialization()方法
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization方法:beanName=springBean
【TargetBean】调用InitializingBean.afterPropertiesSet()
【TargetBean】调用<bean>的init-method属性指定的初始化方法
【实现BeanPostProcessor接口的Bean后处理器】调用BeanPostProcessor.postProcessAfterInitialization()方法
【实现InstantiationAwareBeanPostProcessor接口的Bean后处理器】调用InstantiationAwareBeanPostProcessor.postProcessAfterInitialization方法:beanName=springBean
---------------------容器初始化成功---------------------
TargetBean.toString()=TargetBean[ desc=descValue ]
---------------------现在开始关闭容器!---------------------
【TargetBean】调用DiposibleBean.destory()
【TargetBean】调用<bean>的destroy-method属性指定的初始化方法

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!