To configure a custom bean lifecycle in Spring, put initialization logic in a method annotated with @PostConstruct, put cleanup logic in a method annotated with @PreDestroy, or name the methods in @Bean(initMethod = "...", destroyMethod = "..."). Spring calls the init method after the bean is constructed and its dependencies are injected, and it calls the destroy method when the application context closes.
In Spring Framework 6 and Spring Boot 3 and later, the annotations come from the jakarta.annotation package, not javax.annotation. The old javax imports no longer compile against current Spring versions, which is the most common reason a copied example fails.
@PostConstruct and @PreDestroyInitializingBean and DisposableBeaninitMethod and destroyMethod on @BeanSmartLifecycleBeanPostProcessorFor a singleton bean, Spring runs the lifecycle steps in a fixed order. Knowing the order tells you which callback to use for which job.
*Aware callbacks such as BeanNameAware and ApplicationContextAware.BeanPostProcessor.postProcessBeforeInitialization runs. This is where @PostConstruct is processed.@PostConstruct method runs.InitializingBean.afterPropertiesSet() runs.initMethod runs.BeanPostProcessor.postProcessAfterInitialization runs (proxies such as @Transactional are usually created here).On shutdown the destruction callbacks run in this order: @PreDestroy, then DisposableBean.destroy(), then the custom destroyMethod.
@PostConstruct and @PreDestroyThese two annotations are the recommended approach for most application code. They keep the bean free of Spring-specific interfaces and the intent is visible next to the method.
Rules for these methods: they must take no arguments, may have any visibility, and should not throw checked exceptions. If @PostConstruct throws, the bean fails to start and so does the application context. Keep the work short and do not call other beans that may not be fully initialized yet; for work that needs the whole context, listen for ApplicationReadyEvent instead.
InitializingBean and DisposableBeanThese interfaces do the same job as the annotations but tie your class to Spring.
Use them mainly in framework or library code that must not depend on jakarta.annotation. In application code, prefer the annotations.
initMethod and destroyMethod on @BeanWhen the class is third-party code you cannot annotate, name its methods in the @Bean definition.
Spring also infers a destroy method for @Bean definitions: if the returned object has a public no-argument close() or shutdown() method, Spring calls it automatically. Set destroyMethod = "" to turn that inference off, for example when the container does not own the resource.
SmartLifecycle@PostConstruct runs during bean creation. If you need to start something (a poller, a listener, a scheduler) after all beans exist and stop it in a defined order on shutdown, implement SmartLifecycle.
isAutoStartup() returns true by default, so Spring starts the bean when the context refreshes and stops it on close. Beans with a lower phase value start earlier and stop later.
BeanPostProcessorA BeanPostProcessor receives every bean before and after initialization, which is how Spring itself implements @PostConstruct and AOP proxies. Use it for cross-cutting behaviour.
Declare a processor that is registered through a @Bean method as static so it can be created early without instantiating its configuration class.
| Scope | Init callbacks | Destroy callbacks |
|---|---|---|
| singleton | run once at context start | run at context close |
| prototype | run on every getBean call | never run; you must clean up yourself |
| request / session | run per instance | run when the request or session ends |
A plain Java application must close the context for @PreDestroy to run. Spring Boot registers a JVM shutdown hook for you; with a manually created AnnotationConfigApplicationContext, call registerShutdownHook() or close(). A process killed with kill -9 runs no callbacks at all.
Yes. Spring runs all of them in the documented order, and if the same method name is registered through several mechanisms it is called only once. Mixing them is rarely worth the confusion, so pick one.
@PostConstruct method not called?Usually the bean is not managed by Spring (created with new), the import is javax.annotation on a Spring 6 project, or the bean is not in a scanned package. Check that the class is a bean first.
@PreDestroy run for prototype beans?No. Spring hands a prototype to the caller and stops tracking it, so the caller must release its resources.
Use @PostConstruct and @PreDestroy for your own classes, initMethod and destroyMethod for third-party classes, and SmartLifecycle for components that start and stop work in a defined order.
Spring offers annotations, interfaces and @Bean attributes for custom lifecycle logic, and they run in a fixed order around the bean's dependency injection. Use jakarta.annotation.PostConstruct and PreDestroy by default, SmartLifecycle for ordered start and stop, and remember that prototype beans are never destroyed by the container.