Spring BeanFactoryPostProcess Example

BeanFactoryPostProcessor is an interface which Allows for custom modification of an application context’s bean definitions, adapting the bean property values of the context’s underlying bean factory.

A bean implementing BeanFactoryPostProcessor is called when all bean definitions will have been loaded,but no beans will have been instantiated yet. This allows for overriding or adding properties even to eager-initializing beans. This will let you have access to all the beans that you have defined in XML or that are annotated (scanned via component-scan).

Project structure

Point.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
package com.training.javaforlearn;
 
public class Point 
{
	int x;
	int y;
 
	public int getX() 
	{
		return x;
	}
	public void setX(int x) 
	{
		this.x = x;
	}
	public int getY() 
	{
		return y;
	}
	public void setY(int y) 
	{
		this.y = y;
	}
 
}

Triangle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
package com.training.javaforlearn;
 
public class Triangle
 
{
 
	String type;
 
	Point pointa;
	Point pointb;
	Point pointc;
 
 
	public Point getPointa() 
	{
		return pointa;
	}
 
	public void setPointa(Point pointa) 
	{
		this.pointa = pointa;
	}
 
	public Point getPointb()
	{
		return pointb;
	}
 
	public void setPointb(Point pointb) 
	{
		this.pointb = pointb;
	}
 
	public Point getPointc() 
	{
		return pointc;
	}
 
	public void setPointc(Point pointc) 
	{
		this.pointc = pointc;
	}
 
	public String getType() 
	{
		return type;
	}
 
	public void setType(String type)
	{
		this.type = type;
	}
 
	public void draw()
	{
 
		System.out.println("***** triangle drawn ******* " );
 
		System.out.println("point1 "+"("+getPointa().getX()+","+getPointa().getY()+")");
		System.out.println("point2 "+"("+getPointb().getX()+","+getPointb().getY()+")");
		System.out.println("point2 "+"("+getPointc().getX()+","+getPointc().getY()+")");
 
	}
}

Beanfactory.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
package com.training.javaforlearn;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 
public class Beanfactory implements BeanFactoryPostProcessor
{
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) 
			throws BeansException 
	{		
		System.out.println("post process executed ");	
	}
}

Spring6.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
<?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="triangle1" class="com.training.javaforlearn.Triangle">
 
    <property name="pointa" ref="point1"></property>
    <property name="pointb" ref="point2"></property>
    <property name="pointc" ref="point3"></property>
 
    </bean>
 
   <bean id="point1" class="com.training.javaforlearn.Point">
   <property name="x" value="0"></property>
   <property name="y" value="0"></property>
   </bean>    
 
   <bean id="point2" class="com.training.javaforlearn.Point">  
    <property name="x" value="-12"></property>
   <property name="y" value="0"></property>
   </bean>    
 
   <bean id="point3" class="com.training.javaforlearn.Point">
   <property name="x" value="12"></property>
   <property name="y" value="0"></property>
   </bean>    
 
 
  <bean class="com.training.javaforlearn.Beanfactory"/>
 
 
  </beans>

DrawTriangle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
package com.training.javaforlearn;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class DrawTriangle 
{
	public static void main(String[] args) 
	{
		ApplicationContext context=new ClassPathXmlApplicationContext("spring6.xml");
		Triangle triangle=(Triangle)context.getBean("triangle1");
		triangle.draw();
	}
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *