Sub topic 9
The full callback order
This chapter assumes you know the individual callbacks from the earlier chapters: @PostConstruct, @PreDestroy, InitializingBean, DisposableBean, Aware interfaces and BeanPostProcessor. Here you put them in order and check that order by running code.
The creation sequence
For a singleton bean, the container runs these steps in order:
- Instantiate the bean by calling its constructor.
- Inject dependencies and other properties.
- Call the Aware callbacks, such as
setBeanName. - Call
postProcessBeforeInitializationon eachBeanPostProcessor. The built-in processor that runs@PostConstructmethods is one of them. - Call
afterPropertiesSetfromInitializingBean. - Call the custom
initMethodnamed in@Bean. - Call
postProcessAfterInitializationon eachBeanPostProcessor.
Destruction runs in a similar order. @PreDestroy runs first, then DisposableBean.destroy, then the custom destroyMethod.
Observing the order
This bean prints a line in every callback.
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class DemoBean implements BeanNameAware, InitializingBean, DisposableBean {
public DemoBean() { System.out.println("constructor"); }
@Override
public void setBeanName(String name) { System.out.println("setBeanName: " + name); }
@PostConstruct
void postConstruct() { System.out.println("@PostConstruct"); }
@Override
public void afterPropertiesSet() { System.out.println("afterPropertiesSet"); }
@PreDestroy
void preDestroy() { System.out.println("@PreDestroy"); }
@Override
public void destroy() { System.out.println("DisposableBean.destroy"); }
}A post-processor reports when it sees the bean.
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class LoggingPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DemoBean) System.out.println("before initialization: " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DemoBean) System.out.println("after initialization: " + beanName);
return bean;
}
}The Main class starts a context and closes it with try-with-resources.
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
class AppConfig {
}
public class Main {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class)) {
System.out.println("context running");
}
}
}Running it prints:
constructor
setBeanName: demoBean
@PostConstruct
before initialization: demoBean
afterPropertiesSet
after initialization: demoBean
context running
@PreDestroy
DisposableBean.destroyCommon errors
@PostConstruct appears before your own "before initialization" line. Spring implements @PostConstruct with a built-in BeanPostProcessor that is PriorityOrdered. It runs ahead of an unordered processor like yours. So @PostConstruct is part of the "before" phase, not a separate step.
Several other mistakes are common:
- Expecting a proxy inside
@PostConstruct. AOP proxies are usually created inpostProcessAfterInitialization. Inside your init callbacks,thisis still the raw bean. - Relying on destruction for prototypes. The container does not call destroy callbacks on prototype beans, so you must release their resources yourself.
- Seeing no destroy output. Destruction runs only when the context closes. If the JVM exits without closing the context, the lines never print.
Exercise
A bean has a constructor, a @PostConstruct method, afterPropertiesSet, and an initMethod named in @Bean. In what order do they run, and where does postProcessAfterInitialization fit?
Show answer
Constructor, then @PostConstruct, then afterPropertiesSet, then the custom initMethod. postProcessAfterInitialization runs last, after all init callbacks have finished.