What is the difference between implementing the BeanPostProcessor
interface and either using the init
/destroy
method attributes in the XML
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