spring5.3 五:Bean的生命周期源码解析下

Bean的销毁过程

在Bean创建过程中,在最后(初始化之后),有一个步骤会去判断当前创建的Bean是不是。就是在doCreateBean方法中的registerDisposableBeanIfNecessary(beanName, bean, mbd);下面来看下这个方法

protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
		AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
		//requiresDestruction(bean, mbd)判断这个bean有没有需要执行销毁的方法
		if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
			//判断是否是单例
			if (mbd.isSingleton()) {
		//如果单例的bean有销毁的方法  registerDisposableBean这个方法表示 把销毁的方法相关内容和beanName存放到一个map中
				//new DisposableBeanAdapter()一个适配器  适配器模式 创建销毁方法的方式很多  需要进行适配
				registerDisposableBean(beanName, new DisposableBeanAdapter(
						bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));
			}
			else {
				// A bean with a custom scope...
				Scope scope = this.scopes.get(mbd.getScope());
				if (scope == null) {
					throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
				}
				scope.registerDestructionCallback(beanName, new DisposableBeanAdapter(
						bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));
			}
		}
	}

首先判断这个bean如果是非原型的,接着判断requiresDestruction(bean, mbd)有没有定义销毁的逻辑。

protected boolean requiresDestruction(Object bean, RootBeanDefinition mbd) {
		//DisposableBeanAdapter.hasDestroyMethod(bean, mbd) 判断这个bean有没有定义销毁方法
		//hasDestructionAwareBeanPostProcessors() 判断有没有实现 DestructionAwareBeanPostProcessor这个接口的bean
		//DisposableBeanAdapter.hasApplicableProcessors 判断有没有销毁的方法
		return (bean.getClass() != NullBean.class && (DisposableBeanAdapter.hasDestroyMethod(bean, mbd) ||
				(hasDestructionAwareBeanPostProcessors() && DisposableBeanAdapter.hasApplicableProcessors(
						bean, getBeanPostProcessorCache().destructionAware))));
	}

下面分别看下DisposableBeanAdapter.hasDestroyMethod,hasDestructionAwareBeanPostProcessors(),DisposableBeanAdapter.hasApplicableProcessors这个三个方法是如何判断bean有没有定义销毁逻辑的

DisposableBeanAdapter.hasDestroyMethod

	public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {
		//大致过程就是 判断这个bean有没有实现DisposableBean 或者 AutoCloseable接口 有的话说明定义了销毁方法
		if (bean instanceof DisposableBean || bean instanceof AutoCloseable) {
			return true;
		}
		//如果没有进入到这个方法进一步判断
		return inferDestroyMethodIfNecessary(bean, beanDefinition) != null;
	}

private static String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
		//从beanDefinition获取一个销毁的属性值
		String destroyMethodName = beanDefinition.resolvedDestroyMethodName;
		if (destroyMethodName == null) {
			//获取销毁的方法名
			destroyMethodName = beanDefinition.getDestroyMethodName();
			//这个if表示  如果获取这个销毁的方法名 是 (inferred) 这个字符串的话 就会进一步判断 最后返回销毁的方法名  没有返回""
			if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
					(destroyMethodName == null && bean instanceof AutoCloseable)) {
				// Only perform destroy method inference or Closeable detection
				// in case of the bean not explicitly implementing DisposableBean
				destroyMethodName = null;
				//如果这个bean没有实现DisposableBean的话执行下面逻辑
				if (!(bean instanceof DisposableBean)) {
					try {
						//判断这个bean有没有定义 一个叫close的方法
						destroyMethodName = bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
					}
					catch (NoSuchMethodException ex) {
						try {
							//判断这个bean有没有定义 一个叫shutdown的方法
							destroyMethodName = bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
						}
						catch (NoSuchMethodException ex2) {
							// no candidate destroy method found
						}
					}
				}
			}
			//把销毁的方法名放入到resolvedDestroyMethodName这个属性中  说明已经经过判断
			beanDefinition.resolvedDestroyMethodName = (destroyMethodName != null ? destroyMethodName : "");
		}
		//返回方法名
		return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
	}

DisposableBeanAdapter.hasDestroyMethod大致过程就是判断bean是不是实现了DisposableBean 或者AutoCloseable接口,如果没有判断beanDefinition有没有配置对应的销毁方法。
hasDestructionAwareBeanPostProcessors()和DisposableBeanAdapter.hasApplicableProcessors

protected boolean hasDestructionAwareBeanPostProcessors() {
		return !getBeanPostProcessorCache().destructionAware.isEmpty();
	}

public static boolean hasApplicableProcessors(Object bean, List<DestructionAwareBeanPostProcessor> postProcessors) {
		if (!CollectionUtils.isEmpty(postProcessors)) {
			for (DestructionAwareBeanPostProcessor processor : postProcessors) {
				if (processor.requiresDestruction(bean)) {
					return true;
				}
			}
		}
		return false;
	}

判断bean有没有实现DestructionAwareBeanPostProcessor接口,如果有的话接着执行DisposableBeanAdapter.hasApplicableProcessors因为这是与的关系,DisposableBeanAdapter.hasApplicableProcessors这个方法中会执行DestructionAwareBeanPostProcessor接口中的requiresDestruction方法,如果返回true表示该bean有对应的销毁逻辑,返回false表示没有。

经历了上述步骤后,所有定义了销毁逻辑的bean,都记录在一个map当中。那么什么时候调用这些销毁的逻辑呢?那么就会有人说bean销毁的时候,那么bean什么时候销毁的呢?有人说垃圾回收的时候,但是垃圾回收并不会触发销毁逻辑,GC是jvm层面的不会触发spring定义的销毁逻辑。
Bean销毁是发生在Spring容器关闭过程中的。比如

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Object obj = context.getBean("goodsServiceImpl");
		GoodsServiceImpl goodsService = (GoodsServiceImpl)obj;
		goodsService.test();
		context.close();

后续调用销毁逻辑都在close方法里面。
在Spring容器关闭过程时:

  1. 首先发布ContextClosedEvent事件
  2. 调用lifecycleProcessor的onCloese()方法
  3. 销毁单例Bean
    i. 遍历disposableBeans
    a. 把每个disposableBean从单例池中移除
    b. 调用disposableBean的destroy()
    c. 如果这个disposableBean还被其他Bean依赖了,那么也得销毁其他Bean
    d. 如果这个disposableBean还包含了inner beans,将这些Bean从单例池中移除掉 (inner bean参考https://docs.spring.io/spring-framework/docs/current/spring- framework-reference/core.html#beans-inner-beans)
    ii. 清空manualSingletonNames,是一个Set,存的是用户手动注册的单例Bean的 beanName
    iii. 清空allBeanNamesByType,是一个Map,key是bean类型,value是该类型所有的 beanName数组 iv. 清空singletonBeanNamesByType,和allBeanNamesByType类似,只不过只存了单例 Bean

到此整个bean的生命周期就结束了。

bean生命周期总结

bean在创建过程中经历以下步骤

  1. 生成BeanDefinition
  2. 合并BeanDefinition
  3. 加载类
  4. 实例化前
  5. 实例化
  6. BeanDefinition的后置处理
  7. 实例化后
  8. 自动注入和处理属性
  9. 执行Aware回调
  10. 初始化前
  11. 初始化
  12. 初始化后
  13. 记录所有定义了销毁逻辑的bean

bean销毁的步骤

  1. 首先发布ContextClosedEvent事件
  2. 调用lifecycleProcessor的onCloese()方法
  3. 销毁单例Bean

spring生命周期的扩展点

在bean的生命周期中,spring提供了一些扩展点,可以干预到bean的创建和销毁,注解类型例如

@PostConstruct 标注该注解的方法会在 初始化前执行
@PreDestroy 标注该注解的方法会在bean销毁的时候执行

接口类型的例如BeanPostProcessor及其子类接口,InitializingBean这只用xxxbean接口。
扩展点有很多,下面通过代码来展示一些扩展点

//扫描路劲,换成自己项目的包路径
@ComponentScan(basePackages = {"service","config"})
public class AppConfig {}

@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, DestructionAwareBeanPostProcessor {
	/**
	 * 实例化前
	 * @param beanClass
	 * @param beanName
	 * @return
	 * @throws BeansException
	 */
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if ("goodsServiceImpl".equals(beanName)){
			System.out.println("-----goodsServiceImpl实例化前----" );
		}
		if ("orderServiceImpl".equals(beanName)){
			System.out.println("-----orderServiceImpl实例化前----" );
		}
		return null;
	}

	/**
	 * 实例化后
	 * @param bean
	 * @param beanName
	 * @return
	 * @throws BeansException
	 */
	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if ("goodsServiceImpl".equals(beanName)){
			GoodsServiceImpl goodsService = (GoodsServiceImpl)bean;
			goodsService.bbb();
		}
		return true;
	}

	/**
	 * 依赖注入和属性处理
	 * @param pvs
	 * @param bean
	 * @param beanName
	 * @return
	 * @throws BeansException
	 */
	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		if ("goodsServiceImpl".equals(beanName)){
			System.out.println("goodsServiceImpl:属性处理 @Resource @Autowired");
		}
		return pvs;
	}

	/**
	 * 初始化前
	 * @param bean
	 * @param beanName
	 * @return
	 * @throws BeansException
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if ("goodsServiceImpl".equals(beanName)){
			System.out.println("-----goodsServiceImpl初始化前----" );
			GoodsServiceImpl goodsService = (GoodsServiceImpl)bean;
			goodsService.goodsTest1();
		}
		return bean;
	}

	/**
	 * 初始化后
	 * @param bean
	 * @param beanName
	 * @return
	 * @throws BeansException
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if ("goodsServiceImpl".equals(beanName)){
			System.out.println("-----goodsServiceImpl初始化后----" );
			GoodsServiceImpl goodsService = (GoodsServiceImpl)bean;
			goodsService.goodsTest2();
		}
		return bean;	}

	/**
	 * BeanDefinition的后置处理
	 * @param rootBeanDefinition
	 * @param aClass
	 * @param beanName
	 */
	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition rootBeanDefinition, Class<?> aClass, String beanName) {
		if ("goodsServiceImpl".equals(beanName)){
			System.out.println("-----goodsServiceImpl的BeanDefinition后置处理----" );
			rootBeanDefinition.setInitMethodName("aaa");
			rootBeanDefinition.getPropertyValues().add("orderService",new OrderServiceImpl());
		}
	}

	/**
	 * 具体的销毁逻辑 bean 销毁时执行
	 * @param bean
	 * @param beanName
	 * @throws BeansException
	 */
	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
		if ("goodsServiceImpl".equals(beanName)){
			System.out.println("-----goodsServiceImpl的销毁方法执行----" );
		}
	}

	/**
	 * 初始化完成后 通过返回值判断有没有自定义的销毁逻辑
	 * @param bean
	 * @return
	 */
	@Override
	public boolean requiresDestruction(Object bean) {
		return true;
	}
}

service类

@Service
public class OrderServiceImpl {
	public void test(){
		System.out.println("OrderServiceImpl---test");
	}
}

@Service
@Conditional(GoodsConditional.class)
public class GoodsServiceImpl implements SmartInitializingSingleton, InitializingBean, DisposableBean {
	private OrderServiceImpl orderService;

	public void setOrderService(OrderServiceImpl orderService) {
		this.orderService = orderService;
	}

	public void test(){
		System.out.println("GoodsServiceImpl---------test");
		System.out.println(orderService);
	}

	@Override
	public void afterSingletonsInstantiated() {
		System.out.println("创建完成所有单例bean后:执行了GoodsServiceImpl 的 afterSingletonsInstantiated 方法");
	}

	public void aaa(){
		System.out.println("GoodsServiceImpl---------aaaaa");
	}

	public void bbb(){
		System.out.println("GoodsServiceImpl---bbb:实例化后调用了该方法");
	}

	@PostConstruct
	public void goodsTest(){
		System.out.println("GoodsServiceImpl----@PostConstruct---初始化前执行");
	}
	public void goodsTest1(){
		System.out.println("GoodsServiceImpl----BeanPostProcessor---初始化前执行");
	}
	public void goodsTest2(){
		System.out.println("GoodsServiceImpl----BeanPostProcessor---初始化后执行");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("GoodsServiceImpl----InitializingBean---初始化时执行");
	}

	@PreDestroy
	public void preDes(){
		System.out.println("GoodsServiceImpl----销毁执行preDes");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("GoodsServiceImpl----销毁执行destroy");
	}
}

条件判断类

public class GoodsConditional implements Condition {
	@Override
	public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
		//业务判断
		System.out.println("11111");
		return true;
	}
}

测试类

public class Test {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Object obj = context.getBean("goodsServiceImpl");
		GoodsServiceImpl goodsService = (GoodsServiceImpl)obj;
		goodsService.test();
		context.close();
	}
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值