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

前端 未结 6 1243
不思量自难忘°
不思量自难忘° 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

    Above answers clearly explains some of the very important aspect.

    Apart from that it's also important to understand that both beanPostProcessor and init and destroy methods are part of the Spring bean life cycle.

    BeanPostProcessor class has two methods.

    1) postProcessBeforeInitialization - as name clearly says that it's used to make sure required actions are taken before initialization. e.g. you want to load certain property file/read data from the remote source/service.

    2) postProcessAfterInitialization - any thing that you want to do after initialization before bean reference is given to application.

    Sequence of the questioned methods in life cycle as follows :

    1) BeanPostProcessor.postProcessBeforeInitialization()

    2) init()

    3) BeanPostProcessor.postProcessAfterInitialization()

    4) destroy()

    You may check this by writing simple example having sysout and check their sequence.

提交回复
热议问题