Junior — Middle
Does the Spring container control the lifecycle of a Bean instance declared with scope='prototype'?
sobes.tech AI
Answer from AI
A Spring container does not control the full lifecycle of Bean instances with the prototype scope. In this scope, Spring creates a new bean instance each time it is requested, but after creation and initialization, the container does not manage the further lifecycle of the object — its destruction and other lifecycle stages must be managed by the client code.
This differs from singleton beans, where Spring manages the full lifecycle, including init and destroy methods.
If you need to manage the lifecycle of a prototype bean, you can use the DisposableBean interface and manually call cleanup methods, or apply custom solutions.