Sobes.tech
Junior — Middle

Is the @PreDestroy annotation applicable to beans with prototype scope in Spring?

sobes.tech AI

Answer from AI

The @PreDestroy annotation is not automatically applied to beans with prototype scope in Spring. This is because Spring manages the lifecycle of singleton beans by calling methods with @PreDestroy when the context is destroyed. For prototype beans, Spring creates and passes them to the client but does not track their further lifecycle, so @PreDestroy methods are not called automatically. If cleanup is needed for a prototype bean, it must be done manually by the client, for example, by explicitly calling a cleanup method.