1

in the BeanFactory documentation from spring they ordered 1. to 14. points.

But where you would order @PostContruct into it?

Ringo777
  • 97
  • 7

2 Answers2

1

It runs just before afterPropertiesSet.

zlaval
  • 1,941
  • 1
  • 10
  • 11
1

Although it is not clearly mentioned in the java doc, it seems that @PostConstruct does come before or has same order postProcessBeforeInitialization methods of BeanPostProcessors. And it definitely comes after ServletContextAware's setServletContext and before InitializingBean's afterPropertiesSet. So you can assume it order of 11.

I ran the following code and the following logs:

MyBean instance created
Calling post construct
Called postProcessAfterInitialization() for :myBean
13:00:26.269 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@17695df3, started on Thu Jun 25 13:00:26 IST 2020
public class MyApplication {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(MyConfiguration.class);
        ctx.getBeanFactory().addBeanPostProcessor(new CustomBeanPostProcessor());
        ctx.refresh();


        ctx.close();
    }
public class MyBean {

  public MyBean() {
    System.out.println("MyBean instance created");
  }

  @PostConstruct
  private void init() {
    System.out.println("Calling post construct");
  }

}
public class MyConfiguration {
  @Bean
  @Scope(value="singleton")
  public MyBean myBean() {
    return new MyBean();
  }

}
Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37