Java – spring: how to cleanly terminate prototype scoped beans?

According to spring documentation, spring does not manage the full life cycle of its objects when beans are qualified as "prototypes" More specifically, do not call a break lifecycle callback The client code must perform the required cleanup The spring documentation also recommends using a custom bean postprocessor for this purpose However, the "beanpostprocessor" interface only includes callback methods before and after initializing the bean There is no way to resolve callbacks So where and how to release the resources obtained by the prototype scoped beans?

Solution

What you are looking for is the destructionawarebeanpostprocessor, which is a sub interface of beanpostprocessor

You can create a new implementation of the interface yourself, or use one of its implementation classes, such as commonannotationbeanprocessor

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>