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