Reputation: 10405
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?
Upvotes: 13
Views: 5580
Reputation: 4712
The only clean way to terminate prototype-scoped bean is to explicitly call some of its "destroy"-methods to dispose resources. You can also use Phantom References. Here is more info on different types of references.
Upvotes: 1
Reputation: 2479
What you're looking for is the DestructionAwareBeanPostProcessor, it's a sub-interface of BeanPostProcessor.
You could create a new implementation of that interface yourself, or use one of its implementing classes, like CommonAnnotationBeanProcessor.
Upvotes: 3