Spring Bean销毁过程 5.3.10

Bean的销毁是发生成Spring容器关闭过程中的。比如

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//容器关闭
context.close();

单列Bean的销毁过程中执行了哪些方法?

如果Bean实现了DisposableBean接口,则执行其destory方法。

@Component
public class DestroyService implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean destroy test");
    }
}

输出结果:DisposableBean destroy test

如果Bean实现了AutoCloseable接口,则执行其close方法。

@Component
public class DestroyService implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("AutoCloseable close test");
    }
}

输出结果:AutoCloseable close test

如果Bean有@PreDestroy注解修饰的方法,则执行该方法,可以有多个。

@Component
public class DestroyService {
    
    @PreDestroy
    public void test1() {
        System.out.println("@PreDestroy test 1");
    }

    @PreDestroy
    public void test2() {
        System.out.println("@PreDestroy test 2");
    }

}

输出结果:

@PreDestroy test 1

@PreDestroy test 2

如果BeanDefinition中指定了销毁方法,则执行该方法。如果指定的销毁方法为"(inferred)",则执行close方法,如果close方法不存在,则会执行shutdown方法。

//借助BeanDefinition后置处理器指定销毁方法为test
@Component
public class MergedBeanDefinitionPostProcessorTest implements MergedBeanDefinitionPostProcessor {
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        if (beanName.equals("destroyService")) {
            beanDefinition.setDestroyMethodName("test");
        }
    }
}
@Component
public class DestroyService {
    public void test() {
        System.out.println("BeanDefinition destroyMethod test");
    }
}

输出结果:BeanDefinition destroyMethod test

//借助BeanDefinition后置处理器指定销毁方法为"(inferred)"
@Component
public class MergedBeanDefinitionPostProcessorTest implements MergedBeanDefinitionPostProcessor {
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        if (beanName.equals("destroyService")) {
            beanDefinition.setDestroyMethodName("(inferred)");
        }
    }
}
@Component
public class DestroyService {
    
    public void close() {
        System.out.println("beanDefinition destroyMethod (inferred) close test");
    }

    public void shutdown() {
        System.out.println("beanDefinition destroyMethod (inferred) shutdown test");
    }
    
}

输出结果:beanDefinition destroyMethod (inferred) close test

@Component
public class DestroyService {
    
//    public void close() {
//        System.out.println("beanDefinition destroyMethod (inferred) close test");
//    }

    public void shutdown() {
        System.out.println("beanDefinition destroyMethod (inferred) shutdown test");
    }
    
}

输出结果:beanDefinition destroyMethod (inferred) shutdown test

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值