Bean生命周期
文章目录
前言
Bean生命周期简要介绍
一、Bean生命周期
1、BeanFactoryPostProcesser
这个接口是在BeanDefinition都创建完成之后调用来对BeanDefinition进行相关处理。其中一个类及其作用如下
类 | 作用 |
---|---|
PlaceholderConfigurerSupport | Abstract base class for property resource configurers that resolve placeholders in bean definition property values |
就是对bean的属性占位符进行处理
<property name="url" value="jdbc:${dbname:defaultdb}" />
2、实例化Bean
通过反射
3、填充属性
4、执行Init-method
涉及到BeanPostProcesser接口
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//相关处理
return bean;
}
执行init-menthod
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//相关处理,可以实现动态代理(JDK动态代理、CGLIB)
return bean;
}
5、完整Bean对象
6、Bean销毁
aDestructionAwareBeanPostProcessors
->postProcessBeforeDestruction()
b、DisposableBean’s Destroy DisposableBean
是接口
//Interface to be implemented by beans that want to release resources on destruction
public interface DisposableBean {
/**
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
*/
void destroy() throws Exception;
}
c、a custom destroy-method definition
这个接口两个方法如下
用于对Bean进行处理
二、Environment
1、StandardEnvironment----->customizePropertySources()
2、env properties