Spring核心知识点 - Bean的生命周期
Spring Bean生命周期流程图
概念
- 四个阶段:实例化(Instantiation) -> 依赖注入(populate) -> 初始化(Initializing) --(运行时)-> 销毁(destory)
- 两种作用域:
- Bean级别:在Bean上声明,只作用于该Bean
- Aware类接口(BeanNameAware等):可以通过该接口注册一些特殊的属性
- InitializingBean#afterPropertiesSet():用于依赖注入完成后自行修改属性值
- DisposableBean#destroy()
- 配置定义:@Bean(initMethod = "_init", destroyMethod = "_destory") - 等效于@PostConstruct和@PreDestroy注解
- 容器级别:在容器中声明,作用于容器中所有的Bean
InstantiationAwareBeanPostProcessor
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
// 用于实例化之前,如果该方法返回了一个beanClass的对象,则返回该对象而不进入之后的CreateBean阶段(依然会执行BeanPostProcessor.postProcessAfterInitialization)
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
// 完成实例化后执行,如果该方法返回了false,则跳过依赖注入阶段
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
@Nullable
// 依赖注入之前执行,用于自定义的注值
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return null;
}
}
BeanPostProcessor:
public interface BeanPostProcessor {
@Nullable
// 为Bean实例初始化前做一些处理(ApplicationAware类型接口及在此处完成赋值调用)
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
// 完成初始化后执行,(AOP代理即在此方法中执行 - 实现子类:AbstractAutoProxyCreator)
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
示例
/* Test - Spring Bean的生命周期
* 实例化:InstantiationAwareBeanPostProcessor(before + after)
* 依赖注入:InstantiationAwareBeanPostProcessor.postProcessPropertyValues() + Aware接口
* 初始化:BeanPostProcessor(before + after),InitializingBean,init-method
* 销毁:DisposableBean,destory-method
*/
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
Car car = context.getBean("car", Car.class);
car.run();
((AnnotationConfigApplicationContext) context).removeBeanDefinition("car");
}
}
// 0 InstantiationAware处理器 - postProcessBeforeInstantiation() - 对象实例化之前执行
// 1 构造方法
// 2 InstantiationAware处理器 - postProcessAfterInstantiation() - 对象实例化之后执行
// 3 InstantiationAware处理器 - postProcessPropertyValues() - 实现自定义属性注入
// 4 依赖注入
// 5 Obj - 特殊属性(Aware:BeanName,BeanFactory)注入
// 6 BeanPostProcessor处理器 - postProcessBeforeInitialization() - 初始化前执行
// 7 Obj - InitializingBean#afterPropertiesSet()
// 8 Obj - init-method()
// 9 BeanPostProcessor后置处理 - postProcessAfterInitialization() - 初始化后执行
// 10 运行时
// 11 Obj - DisposableBean#destroy()
// 12 Obj - destroy-method()
// InstantiationAwareBeanPostProcessor.class
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if(beanClass == Car.class){
System.out.println("0 InstantiationAware处理器 - postProcessBeforeInstantiation() - 对象实例化之前执行");
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if(bean instanceof Car){
System.out.println(((Car) bean).index.getAndIncrement() + " InstantiationAware处理器 - postProcessAfterInstantiation() - 对象实例化之后执行");
}
return true;
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
if(bean instanceof Car){
System.out.println(((Car) bean).index.getAndIncrement() + " InstantiationAware处理器 - postProcessPropertyValues() - 实现自定义属性注入");
}
return pvs;
}
}
// BeanPostProcessor.class
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Car){
System.out.println(((Car) bean).index.getAndIncrement() + " BeanPostProcessor处理器 - postProcessBeforeInitialization()前置处理");
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Car){
System.out.println(((Car) bean).index.getAndIncrement() + " BeanPostProcessor后置处理 - postProcessAfterInitialization()后置处理");
}
return bean;
}
}
@Bean(initMethod = "init_method", destroyMethod = "destory_method")
public class Car implements InitializingBean, DisposableBean, BeanNameAware {
public final AtomicInteger index = new AtomicInteger(1);
private String beanName;
private Person person;
public Car() {
System.out.println(index.getAndIncrement() + " Obj - 构造方法");
}
@Autowired
public void setPerson(Person person) {
System.out.println(index.getAndIncrement() + " Obj - 依赖注入");
this.person = person;
}
@Override
public void setBeanName(String beanName) {
System.out.println(index.getAndIncrement() + " Obj - 特殊属性(Aware:BeanName,BeanFactory)注入");
this.beanName = beanName;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(index.getAndIncrement() + " Obj - InitializingBean#afterPropertiesSet()");
}
public void init_method(){
System.out.println(index.getAndIncrement() + " Obj - init-method");
}
public void run(){
System.out.println(index.getAndIncrement() + " Obj - 运行时");
}
public void destory_method(){
System.out.println(index.getAndIncrement() + " Obj - destory-method");
}
@Override
public void destroy() throws Exception {
System.out.println(index.getAndIncrement() + " Obj - DisposableBean#destroy()");
}
}
欢迎疑问、期待评论、感谢指点 -- kiqi,愿同您为友
-- 星河有灿灿,愿与之辉