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)
sequenceDiagram
participant C as ApplicationContext
participant F as BeanFactoryPostProcessor
participant B as DoorController
participant P as BeanPostProcessor
C->>F: postProcessBeanFactory(definitions)
Note over F: edits recipes (capacity=20, ${…})
C->>B: 1 new DoorController()
C->>B: 2 inject (@Autowired setters/fields, properties)
C->>B: 3 setBeanName() (Aware)
C->>P: 4 postProcessBeforeInitialization
C->>B: 5 @PostConstruct
C->>B: 6 afterPropertiesSet()
C->>B: 7 initMethod
C->>P: 8 postProcessAfterInitialization (may return a proxy)
Note over C,B: bean in use
C->>B: 10 @PreDestroy
C->>B: 11 destroy()
C->>B: 12 destroyMethodKey pieces
BeanFactoryPostProcessor: modifies definitions before beans are created (e.g.PropertySourcesPlaceholderConfigurer)BeanPostProcessor:postProcessBeforeInitializationandpostProcessAfterInitialization, for every bean- Initialization:
@PostConstruct→InitializingBean.afterPropertiesSet()→@Bean(initMethod) - Destruction:
@PreDestroy→DisposableBean.destroy()→@Bean(destroyMethod) Awareinterfaces: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 @PostConstructis run by a BeanPostProcessor (CommonAnnotationBeanPostProcessor);@Autowired, by another (AutowiredAnnotationBeanPostProcessor)- AOP and
@Transactionalproxies are created inpostProcessAfterInitialization: the context keeps the proxy, not your object @Beanmethods returning BFPPs (and preferably BPPs) are declaredstatic@PostConstructand@PreDestroycome 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 publicclose()orshutdown()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