Bean Initialisation and Destruction: Hooks and Choosing One
Learn Bean Initialisation and Destruction: Hooks and Choosing One (Phase 3 · Spring Beans (Essential), Spring Core) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Initialisation hooks • Three standard ways to run code after dependency injection: @PostConstruct (Jakarta annotation: preferred, no Spring dependency), InitializingBean.afterPropertiesSet() (Spring interface), and a custom init method ( @Bean(initMethod = "...") / XML init-method ; for third-party classes). They run in that order for the same bean. Use init for validation of required state, opening resources, warming caches, registering with other components. • Do not use the constructor for work that needs injected collaborators (fields are not yet set unless constructor-injected) or that...