What is the difference between BeanPostProcessor and init/destroy method in Spring?

前端 未结 6 1408
不思量自难忘°
不思量自难忘° 2021-01-29 20:34

What is the difference between implementing the BeanPostProcessor interface and either using the init/destroy method attributes in the XML

6条回答
  •  你的背包
    2021-01-29 21:08

    a)The postProcessBeforeInitialization() will be called before initialization of the bean.

    b)Once the bean gets initialized, the different callbacks methods are called in the following order as per the Spring docs:

    1. Methods annotated with @PostConstruct
    2. afterPropertiesSet() as defined by the InitializingBean callback interface
    3. init method defined through the XML.

    The main difference is that the above 3 methods get called after the initialization get completed by the postProcessBeforeInitialization() method.

    Once these methods get completed the method postProcessAfterInitialization() will be called and then the destroy methods are called in the same order:

    1. Methods annotated with @PreDestroy

    2. destroy() as defined by the DisposableBean callback interface

    3. destroy() method defined through the XML.

提交回复
热议问题