PostConstruct called before BeanPostProcessor

I am currently new to spring. I tried to follow the order in which PostConstruct and BeanPostProcessor are called.

According to what I found out, the following is the order: -

  • BPP -> postProcessBeforeInitialization
  • Postcontruct
  • BPP -> postProcessAfterInitialization

However, I see the following order: -

  • Postcontruct
  • BPP -> postProcessBeforeInitialization
  • Postcontruct
  • BPP -> postProcessAfterInitialization

SpringConfig file foo.xml Removed beans context tag: component-scan base-package = "springtest"

@Component
public class MySpring implements ApplicationContextAware,BeanPostProcessor{

public static int temp =0;

public MySpring(){
    System.out.println("Initializing MySpring Constructor");
}

@PostConstruct
public void temp(){
    System.out.println("PostConsturct" + this.getClass());
    temp++;
}

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("Before BPP " + bean.getClass());

    return this;
}

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("After BPP " + bean.getClass());

    return this;
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("Initializing ApplicationContext");

}}

Answer

  • Initializing the MySping Constructor
  • Initializing ApplicationContext
  • PostConsturctclass springtest.MySpring
  • After the properties are set by the springtest.MySpring class
  • Prior to BPP class org.springframework.context.event.EventListenerMethodProcessor
  • PostConsturctclass springtest.MySpring
  • After the properties are set by the springtest.MySpring class
  • After the BPP class springtest.MySpring
  • BPP org.springframework.context.event.DefaultEventListenerFactory
  • PostConsturctclass springtest.MySpring
  • , springtest.MySpring
  • BPP springtest.MySpring

MySpring.temp 3 , PostContruct 3 .

-, , ...

+4
1

, bean MySpring bean.

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("Before BPP " + bean.getClass());

    return this;
}

this, , bean, , MySpring. , bean ApplicationContext.

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigurationBean.class);
ctx.getBean(ConfigurationBean.class);

NoSuchBeanDefinitionException.

bean.

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("Before BPP " + bean.getClass());

    return bean;
}

@PostConstruct BeanPostProcessor, CommonAnnotationBeanPostProcessor. BeanPostProcessor .

ApplicationContext MySpring, CommonAnnotationBeanPostProcessor , , bean. , MySpring , Spring , BeanPostProcessor . CommonAnnotationBeanPostProcessor ( BeanPostProcessor).

+2

Source: https://habr.com/ru/post/1622438/


All Articles