Master Spring Boot Lifecycle: Init & Destroy Callbacks Explained
This article explains Spring Boot’s bean lifecycle callbacks, covering InitializingBean and DisposableBean interfaces, @PostConstruct and @PreDestroy annotations, initMethod and destroyMethod configurations, as well as alternative approaches like SmartInitializingSingleton, ContextRefreshedEvent, and AutoCloseable, with code examples and execution order details.
1. Introduction
Spring provides the InitializingBean and DisposableBean interfaces, which trigger afterPropertiesSet() and destroy() methods during bean initialization and destruction, respectively.
Beyond these, beans can implement the Lifecycle interface to participate in container‑driven start‑up and shutdown processes.
JSR‑250 annotations @PostConstruct and @PreDestroy are considered best practice for receiving lifecycle callbacks without coupling to Spring‑specific interfaces.
2. Initialization Callback
InitializingBean defines a single method that is called after Spring has populated bean properties: void afterPropertiesSet() throws Exception; Example:
public class PersonService implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("PersonService通过InitializingBean接口初始化...");
}
}Note: The official recommendation is to avoid InitializingBean because it tightly couples the bean to Spring. Prefer one of the following:
@PostConstruct annotation
Setting initMethod on @Bean Example using @PostConstruct:
public class PersonService {
@PostConstruct
private void init() {
System.out.println("@PostConstruct init...");
}
}Example using initMethod:
@Bean(initMethod = "init")
public PersonService personService(){
return new PersonService();
}Note: The method referenced by initMethod must have no parameters but may return a value.
For time‑consuming initialization, the recommended approaches are implementing SmartInitializingSingleton or listening to ContextRefreshedEvent.
If a bean implements InitializingBean and also defines an @PostConstruct method and an initMethod, the execution order is:
@PostConstruct method InitializingBean.afterPropertiesSet() Custom
initMethod3. Destruction Callback
Implementing DisposableBean provides a destroy() method that is invoked when the container shuts down: void destroy() throws Exception; Example:
public class PersonService implements DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean ... ");
}
}The destroy() method is called when ApplicationContext.close() is invoked.
Note: Like InitializingBean, DisposableBean is discouraged due to strong coupling. Preferred alternatives are:
@PreDestroy annotation
Specifying destroyMethod on @Bean Example using @PreDestroy:
public class PersonService {
@PreDestroy
private void destroy(){
System.out.println("@PreDestroy ... ");
}
}Example using destroyMethod:
@Configuration
public class AppConfig {
@Bean(destroyMethod = "destroy")
public PersonService personService(){
return new PersonService();
}
}Another option is implementing AutoCloseable:
public class PersonService implements AutoCloseable {
@Override
public void close(){
System.out.println("释放资源");
// TODO
}
}Spring also supports inferred destroy methods by specifying destroyMethod = "(inferred)" on a bean; the container will call a method named close or shutdown if present, provided the bean does not implement DisposableBean.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
