Spring bean的生命周期

bean的生命周期过程的接口

Spring为容器内的bean生命周期提供了大量的扩展接口。可以实现这些接口,在Spring bean生命周期过程中对bean实例进行扩展。。

ApplicationContext中bean的生命周期

Spring中一个bean被创建过程的执行流程。
在这里插入图片描述

BeanFactoryPostProcessor

Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它。同时可以定义多个BeanFactoryPostProcessor,通过设置’order’属性来确定各个BeanFactoryPostProcessor执行顺序。

void postProcessBeanFactory(ConfigurableListableBeanFactory var1) throws BeansException;

在Spring中内置了一些BeanFactoryPostProcessor实现类:

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer : 读取配置文件,在bean初始化之前,根据注解或xml配置给bean设置值。
org.springframework.beans.factory.config.PropertyOverrideConfigurer:似于PropertyPlaceholderConfigurer,PropertyOverrideConfigurer对于bean属性可以有缺省值或者根本没有值
org.springframework.beans.factory.config.CustomEditorConfigurer:注册自定义属性编辑器,用于给bean属性转化类型

InstantiationAwareBeanPostProcessor

InstantiationAwareBeanPostProcessor 继承至BeanPostProcessor,比起BeanPostProcessor。InstantiationAwareBeanPostProcessor多出以下4个方法。主要处理的是bean的实例化过程。

/**
 * 在bean实例化前执行(构造函数执行前)
 * @param beanClass  被实例化bean的class
 * @param beanName  bean的名字
 * @return 返回null就会执行默认的实例化过程,返回Object会替代默认的实例化bean。
* @throws BeansException
 */
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    return null;
}

/**
 * 在bean实例化后执行
 * @param bean  bean被创建的实例对象,实例(依赖Spring注入的属性)属性值还没赋值。
 * @param beanName bean 
 * @return 如果返回true,执行下面的属性值注入。返回false,属性设置行为会被跳过。
 * @throws BeansException
 */
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
    return true;
}

/**
 * 用于给bean处理值注入,@Autowire就在这个过程处理注入
 * @param pvs Spring工厂中存在的属性值
 * @param bean bean的实例对象
 * @param beanName bean的名字
 * @return 返回实例真实设置的值,返回null,继承当前pvs,继续执行后续的postProcessProperties处理值。
 * @throws BeansException
 */
@Nullable
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
    return null;
}

/**
 * Spring5已经过期,用postProcessProperties代替。
 * @param pvs
 * @param pds
 * @param bean
 * @param beanName
 * @return
 * @throws BeansException
 */
@Deprecated
@Nullable
default PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
    return pvs;
} /**
 * 实例化对象前
 *
 */

BeanPostProcessor

BeanPostProcessor有2个方法,扩展bean初始化流程(实例化早于初始化,初始化是为对象赋值,注入属性之类的)。

/**
 * 在类初始化前被调用
 * @param bean  bean的实例对象
 * @param beanName bean的名字
 * @return 返回bean的实例对象,如果返回null,后续的BeanPostProcessors 将不会被调用。
 */
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	return bean;
}

/**
 * bean初始化之后
 * @param bean bean的实例对象
 * @param beanName bean的名字
 * @return 返回bean的实例对象,如果返回null,后续的BeanPostProcessors 将不会被调用。
 * @throws org.springframework.beans.BeansException in case of errors
 */
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	return bean;
}

InitializingBean

实现该接口的bean的所有属性都被设置后执行。

 /**
    * bean的所有属性都被设置完成时执行
 */
void afterPropertiesSet() throws Exception;

DisposableBean

实现该接口的bean的销毁时执行,用于释放资源

void destroy() throws Exception;

FactoryBean

某些bean的实例化过程比较复杂,如果通过配置方式进行可能需要进行非常繁琐的配置。FactoryBean的作用就是用来实现这些bean的实例化。实现FactoryBean接口的bean,不能正常的通过Spring容器使用,通过Spring容器获取的总是它的getObject方法创建的实例。

 /** 
    * 返回一个实例 
 * @return 一个bean实例,可以为null
 * @throws Exception in case of creation errors
 * @see FactoryBeanNotInitializedException
 */
@Nullable
T getObject() throws Exception;

/**
 * @return 返回bean实例的类型
 * @see ListableBeanFactory#getBeansOfType
 */
@Nullable
Class<?> getObjectType();

/**
    *返回由FactoryBean创建的bean实例的作用域是singleton还是prototype,如果isSingleton()返回true,则该实 *例会放到Spring容器中单实例缓存池中
 * @return 对象是否为单例
 * @see #getObject()
 * @see SmartFactoryBean#isPrototype()
 */
default boolean isSingleton() {
	return true;
}

实际执行顺序

@Component
public class UserTest implements BeanNameAware, ApplicationContextAware, BeanPostProcessor, InitializingBean, InstantiationAwareBeanPostProcessor {

    private String name;

    private ApplicationContext applicationContext;

    @Override
    public void setBeanName(String name) {
        System.out.println("setBeanName:" + name);
        this.name = name;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext:" + name);
        this.applicationContext = applicationContext;
    }


    @PostConstruct
    public void init() {
        System.out.println("init:" + name);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet:" + name);
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInstantiation:" + name);
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInstantiation:" + name);
        return false;
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization:" + name);
        return null;
    }



    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization:" + name);
        return null;
    }


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值