Quick revision
The short answer
All three mechanisms do the same job, running code after a bean is initialised and before it is destroyed. If all are present, Spring runs them in a fixed order: at initialisation @PostConstruct, then InitializingBean.afterPropertiesSet(), then the custom init method; at destruction @PreDestroy, then DisposableBean.destroy(), then the custom destroy method. The interfaces couple our class to Spring, while the annotations and @Bean attributes avoid that; @Bean also infers a public close or shutdown method as the destroy method by default, and framework classes often use the interfaces because they are explicit and cheap to call.
What to remember
In this answer, we compared the three lifecycle mechanisms and proved their order: @PostConstruct, afterPropertiesSet() and the custom init method at startup, and @PreDestroy, destroy() and the custom destroy method at shutdown, with each method called only once. We saw that the interfaces couple a class to Spring, so application code prefers the annotations or @Bean attributes, while framework classes use the interfaces for their explicit contract and direct calls. Finally, we saw that @Bean infers a public close or shutdown method as the destroy method, which we can disable with destroyMethod = "".
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
All three mechanisms do the same job, running code after a bean is initialised and before it is destroyed. If all are present, Spring runs them in a fixed order: at initialisation @PostConstruct, then InitializingBean.afterPropertiesSet(), then the custom init method; at destruction @PreDestroy, then DisposableBean.destroy(), then the custom destroy method. The interfaces couple our class to Spring, while the annotations and @Bean attributes avoid that; @Bean also infers a public close or shutdown method as the destroy method by default, and framework classes often use the interfaces because they are explicit and cheap to call.
Why this question is asked
Spring has grown three ways to declare lifecycle callbacks, and real code mixes all of them: our own classes use annotations, framework classes implement interfaces, and third-party classes are wired with @Bean attributes. Interviewers ask for the comparison and the order because mixing them is where surprises come from, such as a method called twice or a resource closed that nobody configured. In this answer, we will recap the three mechanisms, prove the order with one class that uses all of them, look at the trade-offs of each, and then at destroy method inference.
1A Quick Recap of the Three Mechanisms
The first mechanism is a pair of Spring interfaces. InitializingBean has one method, afterPropertiesSet(), which Spring calls once all properties are set. DisposableBean has one method, destroy(), which Spring calls when the bean is destroyed. They are part of Spring itself, in the org.springframework.beans.factory package.
The second is a pair of annotations from Jakarta Annotations, @PostConstruct and @PreDestroy, in jakarta.annotation since Spring Boot 3. They are processed by CommonAnnotationBeanPostProcessor, a post-processor that Spring registers in annotation-based contexts.
The third is configuration: we name the methods when we declare the bean, with @Bean(initMethod = "start", destroyMethod = "stop"), or with init-method and destroy-method in XML. This is the only option for classes we cannot change, such as a library's client class.
2Proving the Order With One Class
Here is one class that uses all three, declared with @Bean so that the custom methods are registered too.
| Phase | Mechanism | Called by |
|---|---|---|
| init 1 | @PostConstruct | CommonAnnotationBeanPostProcessor |
| init 2 | InitializingBean.afterPropertiesSet() | bean factory, invokeInitMethods |
| init 3 | @Bean(initMethod) / init-method | bean factory, invokeInitMethods |
| destroy 1 | @PreDestroy | CommonAnnotationBeanPostProcessor |
| destroy 2 | DisposableBean.destroy() | DisposableBeanAdapter |
| destroy 3 | @Bean(destroyMethod), explicit or inferred | DisposableBeanAdapter |
The reason for this order is where each mechanism is plugged in. @PostConstruct is called by a BeanPostProcessor in its postProcessBeforeInitialization step, which Spring runs before its own init method handling. Then Spring's invokeInitMethods calls afterPropertiesSet() first and the configured init method second. Destruction mirrors this: the destruction-aware post-processors, including the one for @PreDestroy, run first, then destroy(), then the configured method.
There is one more rule worth knowing: Spring calls each method only once. If the custom init method is also named afterPropertiesSet, or a @PreDestroy method is also the configured destroy method, Spring notices and does not call it a second time.
3Coupling: Why the Interfaces Are Not the Default Choice
Implementing InitializingBean or DisposableBean ties our class to Spring. The class cannot be compiled without Spring on the classpath, and a reader sees framework methods mixed into domain code. If we later reuse the class outside Spring, or in a test without a container, the lifecycle methods are just ordinary public methods that nothing calls.
The annotations avoid most of that. They come from a small standard library rather than from Spring, and the same annotations work in other containers. The @Bean attributes avoid it completely: the class knows nothing about any container, and the decision about which method starts or stops it lives in configuration. So for our own application classes, the Spring documentation recommends @PostConstruct and @PreDestroy, and for third-party classes the @Bean attributes.
4Why Framework Classes Still Use the Interfaces
Inside Spring and many libraries, classes such as data source wrappers, message listener containers and factory beans implement InitializingBean and DisposableBean. There are good reasons. The interface is an explicit, compiler-checked contract: a framework class that must validate its settings says so in its type, and nobody can forget to annotate the method. Calling an interface method is a direct call, with no annotation scanning and no reflection, which matters a little for classes created very often and matters for code that must work even when annotation processing is not configured, such as a bare bean factory.
So the choice is a matter of who owns the class. Framework and library authors write code that runs inside Spring by definition, and they value the explicit contract. Application developers write code whose value is its domain logic, and they benefit from keeping Spring out of it.
5Destroy Method Inference for @Bean
One behaviour surprises many developers: for a @Bean, the default destroyMethod is not empty but a special inferred value. At shutdown, Spring looks for a public no-argument method named close or shutdown on the bean's class and calls it. Beans that implement AutoCloseable get their close() called as well, even when they are not declared with @Bean.
This is convenient, because pools and executors are closed without anyone remembering to configure it. It is a problem when the object is owned by somebody else, such as a client shared by a library, because Spring closes it while the library still uses it. Setting destroyMethod to an empty string turns inference off for that bean.
6Follow-up questions
If a class has both @PostConstruct and afterPropertiesSet, which should do what?
In application code, ideally only one of them exists. If both are there, perhaps because a framework base class implements InitializingBean, keep in mind that @PostConstruct runs first, so it must not depend on anything the base class sets up in afterPropertiesSet.
Can an init method have arguments?
No. All three mechanisms call methods with no arguments, because Spring has nothing to pass. Anything the method needs must already be injected. The one small exception is a destroy method, which may take a single boolean parameter in some cases (check your version), but this is rarely used.
Which mechanism runs for a prototype bean?
All the init callbacks run, in the same order, every time a prototype is created. None of the destroy callbacks run, because Spring does not track prototype instances after handing them out.
7Conclusion
In this answer, we compared the three lifecycle mechanisms and proved their order: @PostConstruct, afterPropertiesSet() and the custom init method at startup, and @PreDestroy, destroy() and the custom destroy method at shutdown, with each method called only once. We saw that the interfaces couple a class to Spring, so application code prefers the annotations or @Bean attributes, while framework classes use the interfaces for their explicit contract and direct calls. Finally, we saw that @Bean infers a public close or shutdown method as the destroy method, which we can disable with destroyMethod = "".
Open with
There are three ways to declare lifecycle callbacks. The Spring interfaces InitializingBean and DisposableBean give afterPropertiesSet and destroy. The Jakarta annotations @PostConstruct and @PreDestroy are handled by CommonAnnotationBeanPostProcessor.
Then, if they want more
And the @Bean attributes initMethod and destroyMethod name methods on a class we may not own. If all are present, the order at startup is @PostConstruct, then afterPropertiesSet, then the custom init method, because the annotation is handled in the post-processor's before-initialisation step and Spring's own init step calls the interface before the configured method.
At shutdown it is @PreDestroy, then destroy, then the custom destroy method, and Spring never calls the same method twice. For my own classes I prefer the annotations, because the interfaces couple the class to Spring, and for third-party classes I use the @Bean attributes.
Framework code often uses the interfaces anyway, because they are an explicit contract and a direct call without reflection. One thing to watch is that @Bean infers the destroy method: a public close or shutdown method is called automatically, and AutoCloseable beans are closed too.
That is useful for pools, but for a shared object owned elsewhere I would set destroyMethod to an empty string.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare InitializingBean and DisposableBean with @PostConstruct, @PreDestroy and...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/be4c3484ca/