• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

BeanPostProcessor in Spring

May 19, 2015 By j2eereference Leave a Comment

BeanPostProcessor in Spring

As the name indicate, BeanPostProcessor are classes that tells spring to do some action while initializing the bean.  The BeanPostProcessor interface defines callback methods that you can implement to  process fields that were set, perform validation on a bean etc. You can also implement some custom logic after the Spring container finishes instantiating.

BeanPostProcessor interface has two methods.

  • public Object postProcessAfterInitialization(Object arg0, String arg1)

postProcessAfterInitialization method accept two parameters , First is the bean object and second is the Bean Name .This method returns the Bean object

  •  public Object postProcessBeforeInitialization(Object arg0, String arg1)

postProcessBeforeInitialization method accept two parameters , First is the bean object and second is the Bean Name .This method returns the Bean object

Here in this article lets see how to configure the BeanPostProcessor with an example . Lets create a person class which has two member variables name ,age and a method display() to print the name and age values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.j2eereference.spring;
 
public class Person {
 
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display(){
System.out.println("Name is  "+name);
System.out.println("Age is  "+age);
}
}

Spring.xml :

 

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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.xsd">
 
    <bean id="personBean1" class="com.j2eereference.spring.Person">
    <property name="name" value="George"/>
    <property name="age" value="30"/>
    </bean>
    
    <bean id="personBean2" class="com.j2eereference.spring.Person">
    <property name="name" value="Leo"/>
    <property name="age" value="25"/>
    </bean>
    <bean class="com.j2eereference.spring.DisplayNameBeanPostProcessor">
    </bean>
</beans>

Here you can notice that we have added a bean ,com.j2eereference.spring.DisplayNameBeanPostProcessor .

Now lets create the class DisplayNameBeanPostProcessor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.j2eereference.spring;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
public class DisplayNameBeanPostProcessor implements BeanPostProcessor{
 
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("This is After Initilizing bean "+beanName);
return bean;
}
 
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("This is before Initializing bean "+beanName);
return bean;
}
 
}

In this class we have to impement BeanPostProcessor . I have implemented postProcessAfterInitialization and postProcessBeforeInitialization methods to print the respective messages with bean name .

Now lets test this configuration with the below code

1
2
3
4
5
6
7
8
9
10
11
12
package com.j2eereference.spring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
Person person= (Person)context.getBean("personBean1");
person.display();
}
}

Output

May 19, 2015 4:33:43 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71c2812: startup date [Tue May 19 16:33:43 IST 2015]; root of context hierarchy
May 19, 2015 4:33:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]

This is before Initializing bean personBean1
This is After Initilizing bean personBean1
This is before Initializing bean personBean2
This is After Initilizing bean personBean2
Name is  Leo
Age is 25

 

  • postProcessAfterInitialization and postProcessBeforeInitialization methods are getting called while spring initialize the beans.
  • postProcessAfterInitialization and postProcessBeforeInitialization methods are getting called for each and every bean configured in the spring.xml
  • You can have any number of BeanPostProcessor.

 

 

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.