Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring Bean rear processor


May 14, 2021 Spring


Table of contents


Spring -Bean rear processor

The bean postprocessor allows additional processing of the bean before and after the initialization method is called.

BeanPostProcessor defines a callback method that you can implement to provide your own instantiation logic, rely on parsing logic, and so on. You can also implement some custom logical callback methods by inserting one or more BeanPostProcessor implementations into the Spring container to instantiate, configure, and initialize a bean

You can configure BeanPostProcessor to control the order in which these BeanPostProcessor interfaces order properties provided by the Ordered interface implemented by BeanPostProcessor BeanPostProcessor

BeanPostProcessor manipulate bean (or object) instances, which Spring IoC bean instance BeanPostProcessor does their work.

Attention:

ApplicationContext detects bean defined by the implementation of the BeanPostProcessor interface, bean post-processors, and then calls them when appropriate by creating bean in containers.

In your custom BeanPostProcessor implementation class, BeanPostProcessor.postProcessBeforeInitialization(Object, String) and BeanPostProcessor.postProcessAfterInitialization(Object, String) that the naming is accurate

Otherwise, “ The type InitHelloWorld must implement the inherited abstract method BeanPostProcessor.postProcessBeforeInitialization(Object, String) ” on

Example:

The following example shows how ApplicationContext and use BeanPostProcessor

We use the Eclipse IDE in the right place, and then follow these steps to create Spring application:

Steps Describe
1 Create a project named SpringExample and create a src in the com.tutorialspoint
2 Use Add External JARs to add the Spring Spring as explained Spring Hello World Example section.
3 Create Java HelloWorld, InitHelloWorld HelloWorld in the com.tutorialspoint MainApp
4 Create a Beans Beans profile in the src folder Beans.xml
5 The final step is to create the contents of all Java files and bean profiles and run the application as explained below.

Here is the contents .java HelloWorld file:

package com.tutorialspoint;
public class HelloWorld {
  private String message;
  public void setMessage(String message){
    this.message = message;
  }
  public void getMessage(){
    System.out.println("Your Message : " + message);
  }
  public void init(){
    System.out.println("Bean is going through init.");
  }
  public void destroy(){
    System.out.println("Bean will destroy now.");
  }
}

This is BeanPostProcessor which bean before bean You can implement more complex logic before and after initializing the bean because you have two ways to access bean of the built-in bean object.

Here's what initHelloWorld .java file:

package com.tutorialspoint;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("BeforeInitialization : " + beanName);
    return bean; // you can return any other object as well
  }
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("AfterInitialization : " + beanName);
    return bean; // you can return any other object as well
  }
}

Below is the contents .java mainApp file. H ere, you need to register a AbstractApplicationContext Hook() method for closing hook declared in the Abstract registerShutdownHook() class. It will ensure a normal shutdown and call the destroy method.

package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
  public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
    obj.getMessage();
    context.registerShutdownHook();
  }
}

Here are the profile destroy required by the init and destroy .xml methods:

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

  <bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
      init-method="init" destroy-method="destroy">
      <property name="message" value="Hello World!"/>
  </bean>

  <bean class="com.tutorialspoint.InitHelloWorld" />

</beans>

Once you've created the source bean profile, we're ready to run the application. If everything is fine with your application, the following information will be output:

BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.