The standard way to run cleanup code when a Spring bean is destroyed is to annotate a method with @PreDestroy. Spring calls it when the application context closes. Alternatives are implementing DisposableBean, or naming a method in @Bean(destroyMethod = "...") for classes you do not own. Prototype-scoped beans are never destroyed by the container.
In Spring Framework 6 and Spring Boot 3, @PreDestroy lives in jakarta.annotation. The old javax.annotation.PreDestroy import only works on Spring 5 and Spring Boot 2.
@PreDestroy is the simplest option and keeps your class free of Spring interfaces. The method must take no parameters and may have any visibility.
When the context closes, Spring invokes shutdown(), which stops accepting tasks, waits up to ten seconds and then forces termination. Use this pattern for thread pools, open files, sockets and connections owned by the bean.
DisposableBean declares a single destroy() method.
It works the same way but couples your class to Spring, so @PreDestroy is generally preferred. Interfaces are still useful in framework-style library code that must not depend on annotation processing.
For third-party classes you cannot annotate, name the method in the @Bean definition.
Spring infers a destroy method for @Bean definitions by default: if the returned object has a public no-argument close() or shutdown() method, it is called automatically. Implementing AutoCloseable therefore needs no extra configuration. To switch this off, set destroyMethod = "".
The older XML equivalent is destroy-method on the <bean> element, and default-destroy-method on <beans> applies one name to all beans in the file.
If a bean uses more than one mechanism, Spring runs them in this order:
| Order | Mechanism |
|---|---|
| 1 | Method annotated @PreDestroy |
| 2 | DisposableBean.destroy() |
| 3 | Custom destroyMethod / XML destroy-method |
If the same method is named by more than one mechanism, it runs only once. Beans are destroyed in the reverse order of their dependency, so a bean's dependencies are still available when its own destroy callback runs.
A destroy callback only runs if the context is closed in an orderly way.
SIGTERM, Ctrl+C, SpringApplication.exit) triggers callbacks. A kill -9 or a crash does not.context.close() or context.registerShutdownHook().For graceful shutdown of web servers, enable server.shutdown=graceful and set spring.lifecycle.timeout-per-shutdown-phase=30s, so in-flight requests finish before beans are destroyed.
Verify the effect that matters (a pool terminated, a file closed) rather than only that a method printed a message. For lifecycle-aware components that must start and stop in a defined order, consider SmartLifecycle.
Common causes are the wrong import (javax instead of jakarta on Spring 6), a prototype-scoped bean, a forced kill of the process, or an application that never closes its context.
It can, but Spring logs the failure and continues destroying other beans. Handle exceptions inside the method so cleanup of other resources still happens.
Prefer @PreDestroy: it is standard (Jakarta Annotations) and does not tie the class to Spring. Use DisposableBean when annotations are not an option.
Not usually. For @Bean definitions Spring infers close() automatically.
Use @PreDestroy from jakarta.annotation for cleanup in your own beans, @Bean(destroyMethod = "...") for third-party classes, and DisposableBean only when needed. Remember that prototype beans are not destroyed by Spring, and that callbacks run only on an orderly shutdown of the application context.