Spring中Bean的生命周期

1.实例化 BeanFactoryPostProcessor的实现类;
2.执行BeanFactoryPostProcessor的postProcessorBeanFactory方法;

此方法针对所有bean生效
可在此方法中修改BeanDefinition

3.实例化BeanPostProcessor的实现类;
4.实例化InstantiationAwareBeanPostProcessor的实现类;
5.执行InstantiationAwareBeanPostProcessor的postProcessorBeforeInstantiation方法

Apply this BeanPostProcessor before the target bean gets instantiated
此方法针对所有Bean生效
bean实例化前调用

6.执行Bean的构造方法;
7.执行InstantiationAwareBeanPostProcessor的postProcessorPropertyValues方法;

Post-process the given property values before the factory applies them to the given bean
此方法针对所有Bean生效,
属性值应用到bean上之前调用,可以检查、修改属性值

8.为Bean注入属性;
9.如果Bean实现了BeanNameWare接口,执行setBeanName方法;
10.如果Bean实现了BeanFactoryWare接口,执行setBeanFactory方法;
11.执行BeanPostProcessor的postProcessorBeforeInitialization方法;

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks
此方法针对所有Bean生效
在所有其他(InitializingBean/custom init-method)初始化前调用

12.如果Bean实现了InitializingBean接口,执行afterPropertiesSet方法;
13.执行init-method指定的方法;
14.执行beanPostProcessor的postProcessorAfterInitialization方法

apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks
此方法针对所有Bean生效
bean所有初始化(InitializingBean/init_method)调用结束后调用

15.执行InstantizationAwareBeanPostProcessor的postProcessorAfterInstatization方法

此方法针对所有bean生效
bean实例化结束调用

16.执行业务逻辑
17.开始销毁bean
18.如果实现了DisposableBean接口,执行destroy方法;
19.执行destroy-method指定的方法;

你可能感兴趣的:(Spring,bean生命周期)