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

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

    Init and Destroy callback methods are part of Spring bean life cycle phases. The init method is going to be executed after bean instantiation. Similarly, The destroy method is going to be executed before bean finalization.

    We can implement this functionality using implementing interfaces InitializingBean and DisposableBean, or using annotations @postconstruct and @predestroy, or declare the with init-method and destroy-method attributes.

    BeanPostProcessor interface is used for extending the functionality of framework if want to do any configuration Pre- and Post- bean initialization done by spring container.

    For Example: By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, we have to either register CommonAnnotationBeanPostProcessor or specify the in bean configuration file. Here CommonAnnotationBeanPostProcessor is predefined BeanPostProcessor implementation for the annotations. Like:

    @Required enables RequiredAnnotationBeanPostProcessor processing tool
    @Autowired enables AutowiredAnnotationBeanPostProcessor processing tool

提交回复
热议问题