Cheatsheet

Bean lifecycle

Key idea

Spring creates every bean in a fixed order: first it adjusts the recipes (BeanFactoryPostProcessor), then it instantiates, injects, calls the Aware interfaces, passes the bean through the BeanPostProcessors before and after initializing it and, on shutdown, destroys it. Knowing what happens in each phase tells you where to put things.

Diagram (UML)

ApplicationContextLifecycleConfigCapacityPostProcessorClockDoorControllerLoggingBeanPostProcessor

Key pieces

  • BeanFactoryPostProcessor: modifies definitions before beans are created (e.g. PropertySourcesPlaceholderConfigurer)
  • BeanPostProcessor: postProcessBeforeInitialization and postProcessAfterInitialization, for every bean
  • Initialization: @PostConstruct → InitializingBean.afterPropertiesSet() → @Bean(initMethod)
  • Destruction: @PreDestroy → DisposableBean.destroy() → @Bean(destroyMethod)
  • Aware interfaces: BeanNameAware, BeanFactoryAware, ApplicationContextAware

LifecycleConfig.java

@Configuration
public class LifecycleConfig {

    @Bean // static: post-processors are created before every other bean
    public static CapacityPostProcessor capacityPostProcessor() {
        return new CapacityPostProcessor();
    }

    @Bean
    public static LoggingBeanPostProcessor loggingBeanPostProcessor() {
        return new LoggingBeanPostProcessor();
    }

    @Bean
    public static AuditingPostProcessor auditingPostProcessor() {
        return new AuditingPostProcessor();
    }

    @Bean
    public Clock clock() {
        return Clock.systemUTC();
    }

    @Bean(initMethod = "openDoors", destroyMethod = "lockDoors")
    public DoorController doorController() {
        return new DoorController();
    }

    @Bean
    public CheckInDesk checkInDesk() {
        return new CheckInDesk();
    }
}

Remember for the exam

  • Full order: definitions → BFPP → constructor → injection → Aware → BPP before → init (@PostConstruct, afterPropertiesSet, initMethod) → BPP after → ready → destruction (@PreDestroy, destroy, destroyMethod)
  • In the constructor there's no setter or field injection yet: startup work goes in @PostConstruct
  • @PostConstruct is run by a BeanPostProcessor (CommonAnnotationBeanPostProcessor); @Autowired, by another (AutowiredAnnotationBeanPostProcessor)
  • AOP and @Transactional proxies are created in postProcessAfterInitialization: the context keeps the proxy, not your object
  • @Bean methods returning BFPPs (and preferably BPPs) are declared static
  • @PostConstruct and @PreDestroy come from Jakarta (jakarta.annotation), not from Spring

Exam traps

  • A BFPP works with definitions, not instances: if it calls getBean() it creates beans too early and they skip the BPPs
  • Prototypes don't get destruction callbacks
  • With @Bean, a public close() or shutdown() method is used as an inferred destroy method even if you don't declare it
  • Calling startup logic from the constructor with setter dependencies gives a NullPointerException

← Bean lifecycle · Run it