Sobes.tech
Junior — Middle

Will the method annotated with @PreDestroy be called for a Prototype scope bean in Spring upon its termination?

sobes.tech AI

Answer from AI

The method annotated with @PreDestroy will not be called on a Prototype-scoped bean when the application or Spring container shuts down.

This is because Spring manages the full lifecycle of singleton beans, including calling initialization and destruction methods. For prototype beans, Spring creates an instance and hands it to the client, but does not track its further lifecycle, so @PreDestroy methods are not called automatically.

If cleanup is needed for a prototype bean, it must be done by the client that received the bean, by manually invoking the appropriate methods.