Spring Bean的生命周期
-
在创建bean的时候,指定init和detroy方法.
package com.wzl.yanyan.spring01.beanlifecycle; public class Car { public Car(){ System.out.println("this is constructor method..."); } public void init(){ System.out.println("this car init method...."); } public void destroy(){ System.out.println("this is car destroy method..."); } }
package com.wzl.yanyan.spring01.config;
import com.wzl.yanyan.spring01.beanlifecycle.Car;
import org.springframework.context.annotation.Bean;
public class MainConfig4 {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Car beanLifeCycle(){
return new Car();
}
}
- 指定initMethod,detroyMethod.
package com.wzl.yanyan.spring01.beanlifecycle;
import org.springframework.beans.factory.InitializingBean;
public class Shape implements InitializingBean {
public Shape(){
System.out.println("这是shape类的无参构造器");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("执行 初始化bean的方法");
}
}
package com.wzl.yanyan.spring01.config;
import com.wzl.yanyan.spring01.beanlifecycle.Shape;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
public class MainConfig5 {
// @Scope("p")
@Bean
public Shape initBean(){
return new Shape();
}
}
-
实现DisposableBean接口,在容器关闭时,自动调用方法
package com.wzl.yanyan.spring01.beanlifecycle; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Shape implements InitializingBean, DisposableBean { public Shape(){ System.out.println("这是shape类的无参构造器"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("执行 初始化bean的方法"); } @Override public void destroy() throws Exception { System.out.println("this shape bean is destroy method"); } }
第三中方法 通过注解的方式,在bean实例化,并且属性赋值之后,执行的方法.@PreDestroy注解和@PostConstruct注解
- @PreDestroy和@PostConstruct注解,属于javaEE的规范.注解位于 java.xml.ws.annotation包是Java EE的模块的一部分。J2EE已经在Java 9中被弃用,并且计划在Java 11中删除它。关注 JSR250规范,参考连接
Spring 还提供了第四种Java,处理java的生命周期的处理器.
- 处理器,BeanPostProcessor接口
- 在Bean的初始化的前后,进行一些操作.
- 接口有二个方法,postProcessBeforeInitialization在任何初始化方法调用之前,调用这个方法.比如在调用PostConstruct初始化方法,或者调用init-method初始化方法.
- postProcessAfterInitialization 方法是在初始化方法调用结束之后调用这个方法.
Spring 源码查看
-
为什么实现了BeanPostProcessor类的postProcessBeforInitiallization,能够在Bean初始化之前,调用这个方法.弄清楚这个问题,需要查看spring的源码.查看类,AbstractAutowireCapableBeanFactory中的方法,initializeBean,其中调用了方法applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName),这个方法就是自定义的postProcessBeforInitiallization.接下来会执行语句:
invokeInitMethods(beanName, wrappedBean, mbd);
invokeInitMethods方法就是进行组件初始化赋值的方法.查看后续语句:
if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); }
applyBeanPostProcessorsAfterInitialization方法,即方法postProcessAfterInitialization ,在Bean初始化之后调用.接着查看applyBeanPostProcessorsBeforeInitialization方法:
@Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
查看方法applyBeanPostProcessorsAfterInitialization:
@Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
遍历组件中,所有的postProcessBeforinitiallization,初始化前置处理器,便利组件中,所有的applyBeanPostProcessorsAfterInitialization,初始化后置处理器.注意,如果其中有一个方法返回null,整个方法就会停止执行
在执行initializeBean这个方法之前,会执行populateBean(beanName, mbd, instanceWrapper);这个方法.populateBean这个方法的作用是给熟悉进行赋值
研究一下 BeanPostProcessor这个类.
- spring 底层对BeanPostProcessor的使用.
- Bean的赋值,使用了BeanPostProcessor.
- AutoWired 注解
- 生命周期注解
- @Aysnc,xxx等