Spring Core | BeanFactoryPostProcessor | Example
In Spring, the BeanFactoryPostProcessor is a functional interface that contains a abstract method postProcessBeanFactory. It allows us to modify the Spring context’s bean definitions before any beans get created.
BeanFactoryPostProcessor can create new bean definitions or modify existing ones.
Since BeanFactoryPostProcessor should be called before other bean types are formed, it must be registered as a static method level.
BeanFactoryPostProcessor example
We are creating a simple maven project. You could clone the code from our GitHub repo.
Final Project Directory
Complete pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-beanfactorypostprocessor-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-beanfactorypostprocessor-example</name>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.0</version>
</dependency>
</dependencies>
</project>
Create ABean
package com.knf.dev.demo;
public class ABean {
String aProperty;
public void setAProperty(String aProperty) {
this.aProperty = aProperty;
}
public void doStuff() {
System.out.println("aProperty: " + aProperty);
}
}
Create CustomABeanFactoryPostProcessor
package com.knf.dev.demo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
public class CustomABeanFactoryPostProcessor
implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
System.out.println("Inside 'A'postProcessBeanFactory");
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(ABean.class);
bd.getPropertyValues().add("aProperty", "A property");
((DefaultListableBeanFactory) beanFactory)
.registerBeanDefinition("myABean", bd);
}
}
Create Config
package com.knf.dev.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public static CustomABeanFactoryPostProcessor myBean() {
return new CustomABeanFactoryPostProcessor();
}
}
Create BBean
package com.knf.dev.demo;
public class BBean {
String bProperty;
public void setBProperty(String bProperty) {
this.bProperty = bProperty;
}
public void doStuff() {
System.out.println("bProperty: " + bProperty);
}
}
Create CustomBBeanFactoryPostProcessor
package com.knf.dev.demo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomBBeanFactoryPostProcessor {
@Bean
public static BeanFactoryPostProcessor beanFactoryPostProcessor() {
return beanFactory -> {
System.out.println("Inside 'B'postProcessBeanFactory");
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(BBean.class);
bd.getPropertyValues().add("bProperty", "B property");
((DefaultListableBeanFactory) beanFactory)
.registerBeanDefinition("myBBean", bd);
};
}
}
Create Driver class for testing - TestBeanFactoryPostProcessor
- Type
- Name
- Name and Type.
package com.knf.dev.demo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestBeanFactoryPostProcessor {
public static void main(String[] args) {
AnnotationConfigApplicationContext contextA =
new AnnotationConfigApplicationContext(Config.class);
ABean aBean = contextA.getBean(ABean.class);
//ABean aBean = (ABean) contextA.getBean("myABean");
//ABean aBean = contextA.getBean("myABean",ABean.class);
aBean.doStuff();
AnnotationConfigApplicationContext context2 =
new AnnotationConfigApplicationContext(
CustomBBeanFactoryPostProcessor.class);
BBean bBean = context2.getBean(BBean.class);
//BBean bBean = (BBean) context2.getBean("myBBean");
//BBean bBean = context2.getBean("myBBean",BBean.class);
bBean.doStuff();
}
}
Console Output:
Inside 'A'postProcessBeanFactory
aProperty: A property
Inside 'B'postProcessBeanFactory
bProperty: B property
Inside 'A'postProcessBeanFactory
aProperty: A property
Inside 'B'postProcessBeanFactory
bProperty: B property