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 Factory Post Processor

BeanFactoryPostProcessor in spring belongs to the package org.springframework.beans.factory.config. BeanFactoryPostProcessor is used to work on bean configuration metadata. BeanFactoryPostProcessor can change bean configuration metadata potentially. It happens before other bean is initialized by spring container. In spring, more than one BeanFactoryPostProcessor can be used and their order of execution can be set by order attribute.

Extension of BeanPostProcessor is BeanFactoryPostProcessor interface which allows direct modification of bean definitions before a bean is instantiated

An ApplicationContext will automatically register and process a bean that implements either of these interfaces, but a BeanFactory would have to have a BeanPostProcessor or BeanFactoryPostProcessor registered with it programatically.

A BeanFactoryPostProcessor lets you modify the actual bean definition instead of the instance as it's created. The PropertyResourceConfigurer and PropertyPlaceholderConfigurer are two very useful BeanFactoryPostProcessors. They let you put placeholders as property values that are then replaced with values from property file. This is very useful for configuring a database connection pool, mail server, etc. Here is some class which used BeanFactoryPostProcessor

  • AspectJWeavingEnabler
  • CustomAutowireConfigurer
  • CustomScopeConfigurer
  • PropertyOverrideConfigurer
  • PropertyPlaceholderConfigurer
  • PropertyResourceConfigurer
  • ServletContextPropertyPlaceholderConfigurer
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.BeansException;
import org.springframework.beans.factory.
config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.
config.ConfigurableListableBeanFactory;

public class Util implements BeanFactoryPostProcessor {

@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory)
throws BeansException {

System.out.println("My Bean Factory Post Processor called");
}
}

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 >>