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?
Asked
Active
Viewed 5,313 times
2 Answers
3
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.

Derek Troy-West
- 2,469
- 1
- 24
- 27
-
3@Raihan Not sure if this is correct. The doc says it applies only to Singletons, which makes sense; because how would Spring know when a prototype bean goes out of scope? For details of this interface and its specific method, check out: http://static.springsource.org/spring/docs/3.0.x/javadoc-api/ – shrini1000 Jul 16 '12 at 07:55
1
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.

szhem
- 4,672
- 2
- 18
- 30