Quick revision
The short answer
SmartLifecycle is an interface for beans that need to be started after the whole context is ready and stopped before it is torn down, such as message listener containers and embedded web servers. Each bean returns a phase number from getPhase(): beans with lower phases start first and stop last, so the order of shutdown is the reverse of startup. isAutoStartup() decides whether the bean starts on context refresh, and stop(Runnable callback) lets a bean finish stopping asynchronously within a per-phase timeout.
What to remember
In this answer, we saw that SmartLifecycle separates starting active work from creating a bean. Spring starts auto-startup lifecycle beans after all singletons are created and stops them before any are destroyed. getPhase() orders them, with lower phases starting first and stopping last, isAutoStartup() decides whether they start on refresh, and stop(Runnable callback) lets a bean drain its work asynchronously within a per-phase timeout. Message listener containers and Spring Boot's embedded web server are the best-known users.
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.
SmartLifecycle is an interface for beans that need to be started after the whole context is ready and stopped before it is torn down, such as message listener containers and embedded web servers. Each bean returns a phase number from getPhase(): beans with lower phases start first and stop last, so the order of shutdown is the reverse of startup. isAutoStartup() decides whether the bean starts on context refresh, and stop(Runnable callback) lets a bean finish stopping asynchronously within a per-phase timeout.
Why this question is asked
Some components should not do anything until every bean is ready: a Kafka listener that starts consuming before the database layer exists will fail on its first message. Equally, at shutdown they should stop doing work before the beans they rely on are destroyed. Spring solves this with lifecycle beans, and interviewers ask about SmartLifecycle because it is how graceful startup and shutdown really work in Spring Boot. In this answer, we will recap the difference between creating a bean and starting it, look at the methods of SmartLifecycle, see how phases order start and stop, and look at asynchronous stopping and where Spring uses this itself.
1Creating a Bean Versus Starting It
Spring separates two things that are easy to confuse. Creating a bean means calling its constructor, injecting dependencies and running @PostConstruct. Starting a bean means telling it to begin active work: open a server socket, start polling a queue, run a background thread. If we start work inside @PostConstruct, it begins while other beans may still be under construction, and there is no matching stop step before destruction begins.
The Lifecycle interface gives a bean start(), stop() and isRunning(). SmartLifecycle extends it with three more ideas: automatic startup, phases and asynchronous stop. When the context refreshes, it first creates all non-lazy singletons, and only then does its lifecycle processor start the SmartLifecycle beans that want to start automatically. When the context closes, the lifecycle processor stops them first, and only after that are singletons destroyed and their @PreDestroy methods called.
2The Methods of SmartLifecycle
A typical implementation keeps a running flag and starts or stops a worker. getPhase() returns an int; the default for SmartLifecycle is Integer.MAX_VALUE, which means start late and stop early, while a plain Lifecycle bean counts as phase 0. isAutoStartup() returns true by default, so the bean starts when the context refreshes; returning false means someone must call start() explicitly, for example from an admin endpoint.
Notice that the work begins in start(), not in the constructor or @PostConstruct. That way it can safely use every other bean, and it gets a stop() call before those beans are destroyed.
3How Phases Order Start and Stop
Phases group lifecycle beans. On startup, Spring starts all beans of the lowest phase, then the next phase, and so on upwards. On shutdown it goes the other way: the highest phase stops first, and the lowest phase stops last. The reasoning is that a bean with a low phase is something others depend on, such as a connection or a server, so it should be the first up and the last down.
Suppose a cache warmer has phase 0, a poller has phase 100 and a web server has phase 1000. The cache is ready before the poller starts, and the server only accepts traffic when both are running. At shutdown the server stops accepting traffic first, then the poller stops, and the cache warmer stops last.
7 messages in order. Play them, or step through them one at a time.
Lower phases start first and stop last.
Beans in the same phase start and stop with no guaranteed order between them, except that Spring respects explicit dependencies: if one lifecycle bean depends on another, the dependency is started first and stopped later.
4Asynchronous Stop With a Timeout
Stopping can take time: a listener container may need to finish processing the current batch of messages. SmartLifecycle has a second stop method, stop(Runnable callback). Spring calls it during shutdown and waits until the bean runs the callback or until a timeout expires. The default implementation simply calls stop() and then the callback. Spring calls stop(callback) on every bean of a phase and then waits for all their callbacks together, so beans in one phase drain at the same time, and the timeout applies to the phase as a whole.
In plain Spring Framework the default timeout is 10 seconds per phase on DefaultLifecycleProcessor. Spring Boot exposes it as the property spring.lifecycle.timeout-per-shutdown-phase, with a default of 30 seconds. If a bean does not call the callback in time, Spring logs a warning and moves on to the next phase, so a stuck bean cannot block shutdown for ever.
5Where Spring Uses SmartLifecycle
We rarely need to write our own, because the components that need it already implement it. Message listener containers for Kafka, JMS and RabbitMQ are SmartLifecycle beans, which is why they begin consuming only after the context is ready and stop before the context is destroyed. In Spring Boot, the embedded web server is started and gracefully shut down by SmartLifecycle beans, so that it accepts requests only when the application is ready and drains requests first at shutdown. Since Spring Framework 6.1, the thread pool task executor and scheduler also take part in lifecycle stopping. The exact phase numbers these components use differ by library and version, so check your version before choosing phases that need to sit between them.
6Follow-up questions
What is the difference between SmartLifecycle and @PostConstruct or @PreDestroy?
@PostConstruct and @PreDestroy belong to one bean's creation and destruction, and they run in dependency order while beans are created or destroyed. SmartLifecycle start and stop run once the whole context is ready and before destruction begins, ordered by phase across all lifecycle beans. Long-running activity, such as polling or serving, belongs in start and stop; setting up the bean's own fields belongs in @PostConstruct.
What happens if a lifecycle bean's start() throws an exception?
The context refresh fails. Spring treats it as a startup failure, closes the context and destroys the beans already created, and the application does not start. That is usually what we want: a listener that cannot connect should stop the application from reporting itself as healthy.
Can we stop and start lifecycle beans while the application runs?
Yes. ConfigurableApplicationContext has start() and stop() methods that call the lifecycle processor, and individual beans can be stopped and started directly, for example a Kafka listener container through the KafkaListenerEndpointRegistry. Spring also uses this in its checkpoint and restore support, where lifecycle beans are stopped before a checkpoint and started again after restore.
7Conclusion
In this answer, we saw that SmartLifecycle separates starting active work from creating a bean. Spring starts auto-startup lifecycle beans after all singletons are created and stops them before any are destroyed. getPhase() orders them, with lower phases starting first and stopping last, isAutoStartup() decides whether they start on refresh, and stop(Runnable callback) lets a bean drain its work asynchronously within a per-phase timeout. Message listener containers and Spring Boot's embedded web server are the best-known users.
Open with
SmartLifecycle is for beans that do active work, like polling a queue or running a server. Spring creates all singletons first, and only then starts the SmartLifecycle beans whose isAutoStartup returns true, which is the default.
Then, if they want more
At shutdown it stops them before destroying any singleton, so their dependencies are still there while they stop. getPhase returns an int that orders them: lower phases start first and stop last, so shutdown is the reverse of startup. The default phase for SmartLifecycle is Integer.MAX_VALUE, so by default it starts late and stops early.
For slow shutdowns there is stop(Runnable callback): the bean stops taking new work, finishes in-flight work, maybe on another thread, and then runs the callback. Spring waits for the callback up to a timeout per phase, 10 seconds by default in the framework and 30 seconds in Spring Boot through spring.lifecycle.timeout-per-shutdown-phase.
Kafka, JMS and Rabbit listener containers use this, and so does Spring Boot's embedded web server for graceful shutdown. So in my own code I put long-running work in start and stop instead of @PostConstruct.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- 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 "What is SmartLifecycle, and how do phases control start and stop order?": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/b8497e5d8b/