Spring容器中Bean的生命周期

圣诞节在家整理了一下Spring容器中的Bean到底是怎么诞生的,又是如何毁灭的

容器中Bean的生命周期

下图为Spring容器中Bean的生命周期

image

容器中Bean的创建过程

下图是Spring容器创建一个Bean大致的过程

image

MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition

下图是 CommonAnnotationBeanPostProcessor / AutowiredAnnotationBeanPostProcessor 在此刻做的事情

image

CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition

  1. 将@PostConstruct方法缓存至lifecycleMetadataCache(LifecycleMetadata的initMethods)
  2. 将@PreDestroy方法缓存至lifecycleMetadataCache (LifecycleMetadata的destroyMethods)
  3. 将@Resouorce、@WebServiceRef、@EJB 修饰的字段或方法缓存至injectionMetadataCache(ResourceElement/WebServiceRefElement/EjbRefElement)

AutowiredAnnotationBeanPostProcessor#postProcessMergedBeanDefinition

将@Autowired、@Value、@javax.inject.Inject 修饰的字段或方法缓存至 injectionMetadataCache(AutowiredFieldElement/AutowiredMethodElement)

InstantiationAwareBeanPostProcessor#postProcessProperties

下图是 CommonAnnotationBeanPostProcessor / AutowiredAnnotationBeanPostProcessor 在此刻做的事情

image

CommonAnnotationBeanPostProcessor#postProcessProperties

获取injectionMetadataCache中的InjectedElement(ResourceElement/WebServiceRefElement/EjbRefElement )
并进行解析注入

AutowiredAnnotationBeanPostProcessor#postProcessProperties

获取injectionMetadataCache中的InjectedElement(AutowiredFieldElement/AutowiredMethodElement)
并进行解析注入

BeanPostProcessor#postProcessBeforeInitialization

下图是 CommonAnnotationBeanPostProcessor 在此刻做的事情

iamge

@PostConstruct方法此刻被调用

AbstractAutowireCapableBeanFactory#invokeAwareMethods

做的事情如图

iamge

  1. BeanNameAware.setBeanName
  2. BeanClassLoaderAware.setBeanClassLoader
  3. BeanFactoryAware.setBeanFactory

AbstractAutowireCapableBeanFactory#invokeInitMethods

做的事情如图

iamge

  1. 执行InitializingBean.afterPropertiesSet方法
  2. 执行invokeCustomInitMethod,即执行init-method

BeanPostProcessor#postProcessAfterInitialization

Spring中的@Async

AbstractBeanFactory#registerDisposableBeanIfNecessary

做的事情如图

image

  1. DisposableBean类型的Bean(DisposableBeanAdapter)被放至 DefaultSingletonBeanRegistry的disposableBeans中
  2. 过滤 DestructionAwareBeanPostProcessor 至 DisposableBeanAdapter的beanPostProcessors

容器中Bean的销毁过程

下图是Spring容器中的Bean销毁过程

image

AbstractApplicationContext#doClose

程序正常退出后,系统会调用此方法

DestructionAwareBeanPostProcessor#postProcessBeforeDestruction

下图是CommonAnnotationBeanPostProcessor在此刻做的事情

iamge

执行invokeDestroyMethods,即执行@PreDestroy方法

DisposableBeanAdapter#invokeCustomDestroyMethod

执行invokeCustomDestroyMethod,即执行destroy-method方法

事例

@Service
public class BeanPropertyResource {

    @Override
    public String toString() {
        return "BeanPropertyResource Instantiation";
    }
}

@Service
public class BeanPropertyAutowired {

    @Override
    public String toString() {
        return "BeanPropertyAutowired Instantiation";
    }
}


public class BeanLifeCycle implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {

    @Resource
    private BeanPropertyResource beanPropertyResource;

    @Autowired
    private BeanPropertyAutowired beanPropertyAutowired;

    public BeanLifeCycle() {
        System.out.println("BeanLifeCycle");
        System.out.println("without beanPropertyResource: " + beanPropertyResource);
        System.out.println("without beanPropertyAutowired: " + beanPropertyAutowired);
    }

    @PostConstruct
    public void postConstructMethod() {
        System.out.println("with beanPropertyResource: " + beanPropertyResource);
        System.out.println("with beanPropertyAutowired: " + beanPropertyAutowired);
        System.out.println("postConstructMethod");
    }

    public void initMethod() {
        System.out.println("init-method initMethod");
    }


    @Override
    public void afterPropertiesSet() {
        System.out.println("InitializingBean afterPropertiesSet");
    }


    @PreDestroy
    public void preDestroyMethod() {
        System.out.println("preDestroyMethod");
    }

    @Override
    public void destroy() {
        System.out.println("DisposableBean destroy");
    }

    public void destroyMethod() {
        System.out.println("destroy-method destroyMethod");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("BeanClassLoaderAware");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware");

    }

    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware");
    }

}

XML中配置

<bean class="cn.oyo.trade.starter.BeanLifeCycle" init-method="initMethod" destroy-method="destroyMethod" />

参考

我的博客

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值