@PreDestroy marks a method that the container calls once, just before it destroys a managed bean, so you can release resources such as connections, thread pools, files and scheduled tasks. In Spring it is a standard annotation from Jakarta Annotations (jakarta.annotation.PreDestroy), and Spring runs it when the application context closes or when a singleton bean is removed.
It is the counterpart of @PostConstruct: one method prepares the bean after dependency injection, the other tears it down. The rules below apply to Spring 6 and Spring Boot 3 and later.
A method annotated with @PreDestroy is a lifecycle callback. Spring's CommonAnnotationBeanPostProcessor finds it on each bean and registers it as a destruction callback. The callback runs:
ApplicationContext is closed (for example context.close() or a JVM shutdown hook);SIGTERM and the actuator shutdown endpoint;ConfigurableBeanFactory.destroySingleton(...);request or session reaches the end of that scope.The method is called after the bean has been fully used, while its dependencies are still available. Spring destroys beans in reverse order of their dependencies, so a bean that depends on a data source is destroyed before the data source.
Until Spring Framework 5 and Spring Boot 2, the annotation lived in javax.annotation. Since Jakarta EE 9, Spring Framework 6 and Spring Boot 3, it lives in jakarta.annotation. The old javax.annotation.PreDestroy import no longer works with Spring 6. It was also removed from the JDK itself in Java 11, so older projects had to add a dependency to compile.
| Stack | Import |
|---|---|
| Spring Boot 2.x, Spring 5 | javax.annotation.PreDestroy |
| Spring Boot 3.x, Spring 6 and later | jakarta.annotation.PreDestroy |
In a Spring Boot 3 project the jakarta.annotation-api dependency comes in transitively through the starters, so you normally add nothing.
This component owns a thread pool. It creates the pool after injection and shuts it down cleanly when the application stops.
When the application stops, the console shows:
The method waits for running tasks for up to ten seconds, then interrupts whatever is left. This graceful-then-forced pattern is the usual shape of a cleanup method.
A @PreDestroy method must follow the callback contract:
void.private or package-private, and it must not be static.@PreDestroy is not the only option. Spring offers three mechanisms, and when several are present they run in a fixed order.
| Mechanism | How you declare it | Order |
|---|---|---|
@PreDestroy | Annotation on a method | First |
DisposableBean.destroy() | Implement the interface | Second |
destroyMethod | @Bean(destroyMethod = "close") or XML destroy-method | Third |
For @Bean methods Spring also infers a destroy method: if the returned object has a public no-argument close() or shutdown() method, Spring calls it automatically. Set destroyMethod = "" to turn that inference off.
Prefer @PreDestroy for beans you write yourself, because it keeps the cleanup next to the class and does not tie it to a Spring interface. Use destroyMethod for third-party classes you cannot annotate.
@PreDestroy is not called for prototype-scoped beans. You must clean those up yourself.kill -9, a crash, or the JVM running out of memory) skips every callback. Never rely on @PreDestroy for data you cannot lose; persist important state as you go.main applications that create a context manually must close it. Call context.close() or registerShutdownHook(); Spring Boot registers the shutdown hook for you.For a web application, also consider server.shutdown=graceful and spring.lifecycle.timeout-per-shutdown-phase so in-flight requests finish before beans are destroyed.
The simplest test closes the context and asserts on the side effect.
In a real test, expose the state you need (for example isShutdown()) and assert on it after close().
No. Spring hands a prototype bean to the caller and then forgets it, so it never invokes destruction callbacks. Release the resources yourself, or register a custom BeanPostProcessor that tracks them.
Yes. It is a Jakarta Annotations standard, and Jakarta EE and CDI containers such as Quarkus honour it too. A plain new call does nothing, because a container must invoke the method.
Both run on destruction, but @PreDestroy is a standard annotation with no Spring dependency, while DisposableBean is a Spring interface. If both exist, @PreDestroy runs first.
Common causes are a prototype scope, a process that was killed instead of stopped, a context that was never closed, or an import from the wrong package (javax instead of jakarta on Spring 6).
@PreDestroy gives a managed bean one place to release resources before it is removed. Use jakarta.annotation.PreDestroy on Spring 6 and Boot 3, keep the method parameterless and quick, and remember that it does not run for prototype beans or after a hard kill. Pair it with @PostConstruct, and use destroyMethod when you cannot edit the class.