栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring bean 销毁的 N 种方式 (•̀ᴗ• )

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring bean 销毁的 N 种方式 (•̀ᴗ• )

spring bean 销毁的 N 种方式
    • 嘻嘻 /ᐠ。ꞈ。ᐟ
    • 正文
      • 常用方式
        • @PreDestroy
        • 实现 DisposableBean接口
      • 另辟蹊径
        • destroyMethod
        • AutoCloseable
        • 修改BeanDifinition

嘻嘻 /ᐠ。ꞈ。ᐟ

博主开篇打个广告哈~

博主在 哔哩哔哩 有陆续录制了一些教学视频,有兴趣的小伙伴可以去瞅瞅,求三连~

UP主 肖-信 https://space.bilibili.com/384638478

正文 常用方式 @PreDestroy
	@PreDestroy
	private void preDestory() {
		System.out.println("销毁啦!!德玛西亚");
	}
实现 DisposableBean接口

实现 DisposableBean接口 重写 destroy方法

@Service
public class XianJian implements DisposableBean {
	@PreDestroy
	private void preDestory() {
		System.out.println("销毁啦!!德玛西亚 preDestory");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("销毁啦!!德玛西亚 destroy");
	}
}
另辟蹊径 destroyMethod

定义 Bean 指定 destoryMethod

	@Bean(destroyMethod = "beanDestroyMethod")
	public XianJian xianJian() {
		return new XianJian();
	}
public class XianJian implements DisposableBean {

	@PreDestroy
	private void preDestory() {
		System.out.println("销毁啦!!德玛西亚 preDestory");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("销毁啦!!德玛西亚 destroy");
	}

	public void beanDestroyMethod(){
		System.out.println("销毁啦!!德玛西亚 beanDestroyMethod");
	}
}
AutoCloseable

实现 AutoCloseable 接口 重写 close接口

@Component
public class XianJian implements AutoCloseable{

	public void close() {
		System.out.println("销毁啦!!德玛西亚 close ");
	}

}
修改BeanDifinition

这种方式用的最少…
将 beanDefinition 的 destroyMethodName 设置为 (inferred)

@Component
public class XxxProcessor implements MergedBeanDefinitionPostProcessor {
	
	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) {
		if(beanName.equals("xianJian")){
			beanDefinition.setDestroyMethodName("(inferred)");
		}
	}

	@Override
	public void resetBeanDefinition(String beanName) {
		MergedBeanDefinitionPostProcessor.super.resetBeanDefinition(beanName);
	}
}

然后编写 close 或者 shutdown 方法,若都编写则 close 方法生效。

@Component
public class XianJian {
	public void close() {
		System.out.println("销毁啦!!德玛西亚 close ");
	}

	public void shutdown() {
		System.out.println("销毁啦!!德玛西亚 shutdown ");
	}
}

最后两种骚操作 源码如下,小伙伴们可以琢磨琢磨~
org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary

private static String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
		String destroyMethodName = beanDefinition.resolvedDestroyMethodName;
		if (destroyMethodName == null) {
			destroyMethodName = beanDefinition.getDestroyMethodName();
			boolean autoCloseable = (bean instanceof AutoCloseable);
			if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
					(destroyMethodName == null && autoCloseable)) {
				// Only perform destroy method inference in case of the bean
				// not explicitly implementing the DisposableBean interface
				destroyMethodName = null;
				if (!(bean instanceof DisposableBean)) {
					if (autoCloseable) {
						destroyMethodName = CLOSE_METHOD_NAME;
					}
					else {
						try {
							destroyMethodName = bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
						}
						catch (NoSuchMethodException ex) {
							try {
								destroyMethodName = bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
							}
							catch (NoSuchMethodException ex2) {
								// no candidate destroy method found
							}
						}
					}
				}
			}
			beanDefinition.resolvedDestroyMethodName = (destroyMethodName != null ? destroyMethodName : "");
		}
		return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
	}

感谢笔芯ꔛꕤ

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号