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

What is the difference between implementing the BeanPostProcessor interface and using the attributes of the init / destroy method in the XML configuration file in Spring or implementing the InitializingBean / DisposableBean interface?

+46
spring
Mar 25 2018-12-25T00:
source share
7 answers

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

The BeanPostProcessor interface defines callback methods that you can implement to provide your own (or override the default container) instance creation logic, dependency logic, etc. if you want to implement some custom logic after the Spring container completes the instantiation, configuration, and initialization of the bean, you can connect one or more BeanPostProcessor implementations.

Thus, essentially, the postProcessBeforeInitialization method defined in BeanPostProcessor is called (as the name indicates) before beans are initialized, and also after postProcessAfterInitialization after the bean is initialized.

The difference between the @PostConstruct , InitializingBean and custom init methods is that they are defined in the bean itself. Their ordering can be found in the Combining Life Cycle Mechanisms section of the Spring documentation.

So basically BeanPostProcessor can be used to create custom instance creation logic for multiple beans, if others are defined based on bean.

+66
Mar 25 '12 at 17:25
source share

The answers clearly explain one of the very important aspects.

In addition, it is important to understand that both the beanPostProcessor and init and destroy methods are part of the Spring bean life cycle.

The BeanPostProcessor class has two methods.

1) postProcessBeforeInitialization - since the name clearly says that it was used to verify the necessary actions before initialization. for example, you want to load a specific properties file / read data from a remote source / service.

2) postProcessAfterInitialization - any thing that you want to do after initialization before the bean link to the application.

The sequence of methods interviewed in the life cycle is as follows:

1) BeanPostProcessor.postProcessBeforeInitialization ()

2) init ()

3) BeanPostProcessor.postProcessAfterInitialization ()

4) destroy ()

You can verify this by writing a simple example with sysout and checking their sequence.

+11
Apr 12 '12 at 10:40
source share

BeanPostProcessor . BeanPostProcessor gives you the ability to process the bean instance created by the IoC container after it is created, and then again after the initialization event occurs in the instance. You can use this to process the fields that have been set, perform a bean check, or even look for values ​​from a remote resource to set to a bean by default.

BeanPostProcessors and any of the beans on which they depend are created before any other beans in the container. After they are created and ordered, they are used to process all other beans, since they are created by the IoC container. Spring various AOP proxies for caching, transactions, etc. all are applied by BeanPostProcessors. Thus, any BeanPostProcessor you created is not suitable for AOP proxies. Since AOP proxies are applied in this way, it is possible that the AOP proxy has not yet been applied to the instance, so care should be taken if this affects the execution of any subsequent processing.

init / destroy method . In Spring, you can use the init-method and destroy-method as an attribute in a bean configuration file for a bean to perform certain actions during initialization and destruction.

Here is an example to show you how to use the init-method and destroy-method.

 package com.xyz.customer.services; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void initIt() throws Exception { System.out.println("Init method after properties are set : " + message); } public void cleanUp() throws Exception { System.out.println("Spring Container is destroy! Customer clean up"); } } 

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="customerService" class="com.xyz.customer.services.CustomerService" init-method="initIt" destroy-method="cleanUp"> <property name="message" value="i'm property message" /> </bean> 

+8
Mar 26 2018-12-12T00:
source share

And another main distinguishing feature is InitializingBean, the afterPropertiesSet () and destory () methods associated with DisposableBean did not accept any parameters and the return type is also void, so we did not implement any custom logic. But resorting to the BeanPostProcess methods postProcessBeforeInitialization (Object bean, String beanName) and postProcessAfterInitilization (Object bean, String beanName), they take these two parameters and return the type also Object, so that we can write initialization logic, as well as any user login based on passing the bean. ..

Both of these callback methods include the bean life cycle, and the next life cycle as follows

1) BeanPostProcessor.postProcessBeforeInitilazation ()

2) @postConstruct or InitializingBean.afterPropertiesSet () or the initialization method, which is defined in xml / * here it also follows the same oredr if three methods are available ** /

3) BeanPostProcessor.postProcessAfterInitialization ()

4) @preDestroy or DisposibleBean.destroy () or destroy the method that is defined in xml / * here it also follows the same oredr if three methods are available ** /

+4
Jul 15 '14 at 10:08
source share

Initiate and destroy callback methods are part of the Spring bean lifecycle phases. Initialization will be performed before the bean is initialized. Similarly, the Destroy method will be executed before the bean is destroyed.

We can implement this functionality by implementing the InitializingBean and DisposableBean interfaces, or by using @postconstruct and @predestroy or by declaring <bean> attributes with the init-method and destroy-method .

The BeanPostProcessor interface is used to extend the functionality of the framework if you want to make any configuration before and after the bean created by the Spring container.

An example . By default, Spring will not know the annotations @postconstruct and @predestroy . To enable it, we need to either register CommonAnnotationBeanPostProcessor , or specify the configuration file <context:annotation-config /> in the bean. Here the CommonAnnotationBeanPostProcessor predefined by the BeanPostProcessor implementation for annotations. For example:

@Required includes the RequiredAnnotationBeanPostProcessor processing tool
@Autowired includes the AutowiredAnnotationBeanPostProcessor processing AutowiredAnnotationBeanPostProcessor

+2
Jan 13 '16 at
source share

A component goes through several stages from creation to destruction in a Spring IoC container. You may need to do some initialization of this component before creating this component. Similarly, some operations may be required before destroying and removing a component from an IoC container. For this Spring infrastructure, you can define component lifecycle management methods.

  • BeanPostProcessor , and the method specified in the init-method is related to the steps of creating the component.

If the component implements the BeanPostProcessor interface, then the Spring IoC container calls the methods of this interface: postProcessBeforeInitialization() , postProcessAfterInitialization() . After that, the component is ready for use and will remain in the IoC container until it is destroyed. The method specified in the init-method attribute will be called between the two methods. (in fact, prior to the afterPropertiesSet() method of the InitializingBean interface, if the component implements this interface).

  • DisposableBean , and the method specified in destroy-method is related to the stages of component destruction.

If the component implements the DisposableBean interface, then the Spring IoC container calls the method of this interface: destroy() . After that, the method specified in the destroy-method attribute will be called. After that, the component is destroyed.

It is preferable to use the methods listed in the attributes. Implementing the BeanPostProcessor and DisposableBean interfaces makes communication between components and the Spring APIs.

0
06 Oct '15 at 12:13
source share

a) The postProcessBeforeInitialization () function is called before the bean is initialized.

b) After the bean is initialized, the various callback methods are called in the following order according to Spring docs:

  • Methods annotated with @PostConstruct
  • afterPropertiesSet (), as defined by the InitializingBean callback interface
  • init method defined via XML.

The main difference is that the above 3 methods are called after completion of the initialization using the postProcessBeforeInitialization () method.

Once these methods are completed, the postProcessAfterInitialization () method is called, and then the destruction methods are called in the same order:

  • Methods annotated with @PreDestroy

  • destroy (), as defined by the DisposableBean callback interface

  • destroy () method defined via XML.

-one
Jul 24 '16 at 2:11
source share



All Articles