Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Bean Post Processor

BeanPostProcessors interface provides methods that you can implement to have your own instantiation logic.Also you can write your own logic after spring IOC finishes instantiating, configuring, and initializing a bean by plugging in one or more BeanPostProcessor implementations.

You can configure multiple BeanPostProcessors and also can decide the order in which they will run relative to each other by setting order property but that BeanPostProcessors have to implement ordered interface.

BeanPostProcessor in spring is called before bean registration to container. Conditional logic can be checked before bean registration to container. addBeanPostProcessor can be used to registers a bean programmatically. Find the example in which the custom BeanPostProcessor has been used.

A BeanPostProcessor gives you a chance to process an instance of a bean created by the IoC container after it's instantiation and then again after the initialization lifecycle event has occurred on the instance. You could use this to process fields that were set, perform validation on a bean, or even look up values from a remote resource to set on the bean as defaults.

Below is a some classes that implement the BeanPostProcessor interface.

  • AbstractAdvisorAutoProxyCreator
  • AutowiredAnnotationBeanPostProcessor
  • BeanNameAutoProxyCreator
  • CommonAnnotationBeanPostProcessor
  • DefaultAdvisorAutoProxyCreator
  • InitDestroyAnnotationBeanPostProcessor
  • LoadTimeWeaverAwareProcessor
  • ServletContextAwareProcessor

Below is the example of BeanPostProcessor. In below example It have two class one is employee and second is util which will use BeanPostProcessor interface. BeanPostProcessor have two method which will use when you create a bean from context.xml file

  • postProcessBeforeInitialization
  • postProcessAfterInitialization
Employee.java

package beanPostProcessor;

public class Employee {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

public void init() {
System.out.println("Bean init method called ");
}

public void destroy() {
System.out.println("Bean destroy method called ");
}
}

Util.java

package beanPostProcessor;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

public class Util implements BeanPostProcessor {

public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean;
}

public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean;
}

}

context.xml

<?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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

<bean id="employee" class="beanPostProcessor.Employee"
init-method="init" destroy-method="destroy" >
<property name="name" value="Test"/>
</bean>

<bean> class="beanPostProcessor.Util"/>

</beans>

Main.java

package beanPostProcessor;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class Main {
public static void main(String[] args) {

AbstractApplicationContext context = new
ClassPathXmlApplicationContext("beanPostProcessor/context.xml");

Employee obj = (Employee) context.getBean("employee");
System.out.println(obj.getName());;
context.registerShutdownHook();
}
}


     << Previous
Next >>