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

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

    This is pretty clearly explained in the Spring documentation about the Container Extension Points.

    The BeanPostProcessor interface defines callback methods that you can implement to provide your own (or override the container's default) instantiation logic, dependency-resolution logic, and so forth. If you want to implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean, you can plug in one or more BeanPostProcessor implementations.

    So in essence the method postProcessBeforeInitialization defined in the BeanPostProcessor gets called (as the name indicates) before the initialization of beans and likewise the postProcessAfterInitialization gets called after the initialization of the bean.

    The difference to the @PostConstruct, InitializingBean and custom init method is that these are defined on the bean itself. Their ordering can be found in the Combining lifecycle mechanisms section of the spring documentation.

    So basically the BeanPostProcessor can be used to do custom instantiation logic for several beans wheras the others are defined on a per bean basis.

提交回复
热议问题