Sub topic 2
Destruction Callbacks, Post-Processors, and Singleton Scope
This chapter assumes core Java, Spring dependency injection, and experience running a Spring application. The focus is what happens after a bean is created and how the default singleton scope affects cleanup.
Destruction callbacks
A destruction callback releases resources when the application context closes. For example, a bean can close a connection pool or stop a worker thread.
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
@Component
class ReportWorker implements DisposableBean {
@PreDestroy
void prepareToStop() {
System.out.println("preparing to stop");
}
@Override
public void destroy() {
System.out.println("worker stopped");
}
}Spring calls @PreDestroy and then DisposableBean.destroy() for a managed singleton. You can also name a cleanup method on a @Bean:
@Bean(destroyMethod = "close")
public Pool pool() {
return new Pool();
}The method must exist on Pool. Spring calls it when the context closes.
Aware interfaces
An Aware interface lets a bean receive information about its container. For example, BeanNameAware supplies the bean's registered name. ApplicationContextAware supplies the application context.
These interfaces are useful for framework integration, but constructor injection is usually clearer for application dependencies.
Customizing every bean with BeanPostProcessor
A BeanPostProcessor runs for many beans, before and after their initialization callbacks. It can inspect, modify, or wrap a bean.
@Component
class LoggingProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(
Object bean, String name) {
System.out.println("before: " + name);
return bean;
}
@Override
public Object postProcessAfterInitialization(
Object bean, String name) {
return bean;
}
}An initialization callback belongs to one bean. A post-processor can apply a rule across the container.
The full callback order
For a typical singleton, the important order is:
The exact behavior can vary when processors wrap beans or when infrastructure beans are involved. The diagram describes the normal component lifecycle.
Singleton scope
Singleton is Spring's default scope. It creates one bean instance per application context, not one instance for the entire JVM. The context usually creates singleton beans eagerly during startup. @Lazy delays creation until the bean is first requested.
When the context closes, Spring invokes registered destruction callbacks for its singleton beans:
try (var context =
new AnnotationConfigApplicationContext(AppConfig.class)) {
context.getBean(ReportWorker.class);
}The try-with-resources block closes the context, which starts singleton destruction.
Exercise
Which callback runs before DisposableBean.destroy() during normal singleton destruction?
Show answer
The method marked with @PreDestroy runs first. A configured custom destroy method follows the disposable callback.