Spring Bean Lifecycle Step by Step

Difficulty: Intermediate

Question

Explain the lifecycle of a Spring bean from creation to destruction. Where do @PostConstruct and @PreDestroy fit in?

Answer

Think of a bean like a new employee joining a company. First the recruiter creates the record, then HR fills in the details, then onboarding runs, the employee works for years, and finally there is an exit process. Spring beans follow a very similar sequence, and knowing the order lets you place initialisation code in the right hook instead of guessing.

Here is the flow for a singleton bean. First, Spring reads the bean definition, which is the recipe. It then instantiates the object by calling the constructor. Next it populates properties, meaning dependencies are injected via setters or fields. Then the Aware callbacks run if you implemented them, such as BeanNameAware, BeanFactoryAware, and ApplicationContextAware. After that, every registered BeanPostProcessor gets its postProcessBeforeInitialization call. Then the actual initialisation methods run in a defined order: the method annotated with @PostConstruct, then afterPropertiesSet if you implement InitializingBean, then any custom init method named in the @Bean annotation. After those, BeanPostProcessors get postProcessAfterInitialization. This last hook is important because it is where Spring wraps your bean in a proxy for @Transactional, @Async, or AOP advice. The bean is now ready and lives in the container.

On shutdown, when the context closes, singleton beans are destroyed: the @PreDestroy method runs first, then DisposableBean.destroy, then any custom destroy method. Spring Boot registers a JVM shutdown hook so this happens on SIGTERM as well, which is why graceful shutdown of connection pools and thread executors works.

Why do we need @PostConstruct at all if we have a constructor? Because inside the constructor, injected fields and setter dependencies may not yet be set. With constructor injection your constructor parameters are available, but with field injection they are null during construction. Also, the bean has not been through post-processors yet. @PostConstruct runs after injection is complete, so it is the safe place to warm a cache, validate configuration, or open a resource. Keep it fast, though: it blocks application startup, and an exception there aborts the entire boot.

Points that catch candidates out: in Boot 3 the annotations come from jakarta.annotation, not javax.annotation. Prototype beans get all initialisation callbacks but no destruction callbacks. And the proxy created in postProcessAfterInitialization means that inside @PostConstruct you are still working with the raw object, so calling a @Transactional method of the same bean there does not start a transaction, because the proxy does not exist yet. That is a subtle bug that appears in real projects when someone loads reference data with @Transactional inside a @PostConstruct.

For a modern alternative to @PostConstruct that needs the entire application ready, consider CommandLineRunner, ApplicationRunner, or an @EventListener on ApplicationReadyEvent. Those run after the context is fully started and proxies are in place, making them safer for tasks that touch the database transactionally.

Code examples

Watching the lifecycle order

@Component
public class LifecycleDemo implements InitializingBean, DisposableBean {

    public LifecycleDemo() { System.out.println("1. constructor"); }

    @PostConstruct
    public void init() { System.out.println("2. @PostConstruct"); }

    @Override
    public void afterPropertiesSet() { System.out.println("3. afterPropertiesSet"); }

    @PreDestroy
    public void cleanup() { System.out.println("4. @PreDestroy"); }

    @Override
    public void destroy() { System.out.println("5. DisposableBean.destroy"); }
}

Annotations are preferred because they avoid coupling your class to Spring interfaces.

Key points

Common follow-up questions

Interview tips

Concepts covered

Bean Lifecycle, PostConstruct, BeanPostProcessor, PreDestroy

More Spring Boot questions