Junior — Middle
What is the behavior of the destroy method for a bean with prototype scope in Spring Framework?
sobes.tech AI
Answer from AI
In Spring Framework, a bean with prototype scope is created anew for each request, and the container does not manage its lifecycle after creation. This means that the destroy method (for example, annotated with @PreDestroy) is not called automatically for prototype beans.
If cleanup or resource release is needed for a prototype bean, it must be done by the client code that obtained the bean from the container, manually calling the appropriate methods.
Unlike singleton beans, for which Spring automatically calls initialization and destruction methods, prototype beans require explicit lifecycle management after creation.