春季:如何彻底终止原型范围内的bean? [英] Spring: How to cleanly terminate prototype-scoped beans?

查看:75
本文介绍了春季:如何彻底终止原型范围内的bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Spring文档,当bean的作用域为原型"时,spring无法管理其对象的完整生命周期.更具体地说,不调用销毁生命周期回调.客户端代码必须进行所需的清理. Spring文档还建议为此使用定制的bean后处理器.但是"BeanPostProcessor"接口仅包含用于bean初始化之前和之后的回调方法.没有用于解构回调的方法.然后在哪里以及如何释放通过原型作用域的bean获得的资源?

According to Spring documentation when a bean is scoped as "prototype" spring does not manage the complete lifecycle of its objects. More specifically the destruction lifecycle callbacks are not called. The client code must do the required clean ups. The spring documentation also suggests to use a custom bean post-processor for this purpose. But the "BeanPostProcessor" interface only includes callback methods for before and after initialization of beans. There is no method for desctruction callback. Then where and how to release resources obtained by prototype-scoped beans?

推荐答案

您正在寻找的是DestructionAwareBeanPostProcessor,它是BeanPostProcessor的子接口.

What you're looking for is the DestructionAwareBeanPostProcessor, it's a sub-interface of BeanPostProcessor.

您可以自己创建该接口的新实现,也可以使用其实现类之一,例如CommonAnnotationBeanProcessor.

You could create a new implementation of that interface yourself, or use one of its implementing classes, like CommonAnnotationBeanProcessor.

这篇关于春季:如何彻底终止原型范围内的bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆