Bean lifecyclemedium3-5 years

A bean implements both `InitializingBean` and has a `@PostConstruct` method. Which callback runs first, and why does that order surprise almost everyone who guesses?

@PostConstruct runs first, then InitializingBean.afterPropertiesSet(), then any init-method configured on the @Bean definition — and most people guess the opposite, because InitializingBean is the older, more official-looking Spring interface and @PostConstruct looks like a lightweight afterthought. The actual documented order in the bean's creation sequence is: construct, populate (fields and setters), aware callbacks, BeanPostProcessors running before initialisation, then the initialise step itself — @PostConstruct, then afterPropertiesSet, then the init-method, in that order — and only after all of that do the BeanPostProcessors that build proxies run. Both callbacks see a fully populated bean either way; the only thing that actually matters day to day is that neither one sees the finished proxy, because proxying happens after both.

The lesson behind it →
More on Bean lifecycle