Spring - Bean Post Processors


The BeanPostProcessor interface characterizes callback techniques that you can execute to give your very own instantiation rationale, reliance goals rationale, and so forth. You can likewise actualize some custom rationale after the Spring holder gets done with instantiating, arranging, and introducing a bean by connecting at least one BeanPostProcessor implementations.

You can design numerous BeanPostProcessor interfaces and you can control the request in which these BeanPostProcessor interfaces execute by setting the order property gave the BeanPostProcessor actualizes the Ordered interface.

The BeanPostProcessors work on bean (or article) cases, which implies that the Spring IoC holder instantiates a bean occurrence and after that BeanPostProcessor interfaces do their work.

An ApplicationContext consequently distinguishes any beans that are characterized with the execution of the BeanPostProcessor interface and registers these beans as postprocessors, to be then called fittingly by the compartment upon bean creation.

Example

The following precedents tell the best way to compose, register, and use BeanPostProcessors with regards to an ApplicationContext.

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Steps Description
1 Create an undertaking with a name SpringExample and make a bundle com.tutorialspoint under the src organizer in the made project.
2 Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, InitHelloWorld and MainApp under the com.tutorialspoint package.
4 Create Beans design record Beans.xml under the src folder.
5 The last advance is to make the substance of all the Java records and Bean Configuration document and run the application as clarified below.

Here is the substance of HelloWorld.java document −

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 experiencing init."); 

} 

public void destroy(){ 

System.out.println("Bean will demolish now."); 

} 

}

This is an essential case of actualizing BeanPostProcessor, which prints a bean name when instatement of any bean. You can execute increasingly complex rationale when intializing a bean since you approach on bean object inside both the post processor methods.

Here is the substance of InitHelloWorld.java record −

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 restore some other article as well 

} 

public Object postProcessAfterInitialization(Object bean, String beanName) 

throws BeansException { 

System.out.println("AfterInitialization : " + beanName); 

return bean; //you can restore some other item as well 

} 

}

Following is the substance of the MainApp.java record. Here you have to enlist a shutdown snare registerShutdownHook() technique that is pronounced on the AbstractApplicationContext class. This will guarantees a smooth shutdown and calls the pertinent annihilate methods.

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 setting = new ClassPathXmlApplicationContext("Beans.xml"); 

HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); 

obj.getMessage(); 

context.registerShutdownHook(); 

} 

}

Following is the design document Beans.xml required for init and decimate strategies −

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

<beans xmlns = "http://www.springframework.org/construction/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/construction/beans 

http://www.springframework.org/construction/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 are finished with making the source and bean design records, let us run the application. In the event that all is well with your application, it will print the accompanying message −

BeforeInitialization : helloWorld 

Bean is experiencing init. 

AfterInitialization : helloWorld 

Your Message : Hello World! 

Bean will decimate now. 







© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3