Sobes.tech
Junior — Middle

What happens to a method annotated with @PreDestroy if it belongs to a prototype bean in Spring?

sobes.tech AI

Answer from AI

A Spring bean with prototype scope is created anew each time it is requested, and the container does not manage its lifecycle after creation. Therefore, a method annotated with @PreDestroy will not be called automatically for a prototype bean when it is destroyed, as the container does not track its further fate.

If cleanup is necessary for a prototype bean, it must be done manually, for example, by calling the method annotated with @PreDestroy yourself or by using other lifecycle management mechanisms.

What happens to a method annotated with @PreDestroy… - sobes.tech