Close

Spring Framework - Dynamically registering beans Examples

[Last Updated: Nov 9, 2025]

Spring Framework 

Programmatically registering beans with GenericBeanDefinition

package com.logicbig.example;

import java.util.Date;

public class MyBean {
private Date date;

public void doSomething() {
System.out.println("from my bean, date: " + date);
}

public void setDate(Date date) {
this.date = date;
}
}

package com.logicbig.example;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;

import java.util.Date;

public class GenericBeanDefinitionExample {

public static void main(String[] args) {
DefaultListableBeanFactory context =
new DefaultListableBeanFactory();

GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(MyBean.class);

MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("date", new Date());

//alternatively we can use:
// gbd.getPropertyValues().addPropertyValue("date", new Date());
gbd.setPropertyValues(mpv);

context.registerBeanDefinition("myBeanName", gbd);

MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}

Output

from my bean, date: Tue Nov 04 23:18:22 CST 2025
Original Post




Programmatically registering a bean with BeanDefinitionBuilder:

package com.logicbig.example;

public class MyBean {
private String str;

public void setStr(String str) {
this.str = str;
}

public void doSomething() {
System.out.println("from MyBean " + str);
}
}

package com.logicbig.example;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

public class BeanDefinitionBuilderExample {

public static void main(String[] args) {
DefaultListableBeanFactory beanFactory =
new DefaultListableBeanFactory();

BeanDefinitionBuilder b =
BeanDefinitionBuilder.rootBeanDefinition(MyBean.class)
.addPropertyValue("str", "myStringValue");

beanFactory.registerBeanDefinition("myBean", b.getBeanDefinition());

MyBean bean = beanFactory.getBean(MyBean.class);
bean.doSomething();
}
}

Output

from MyBean myStringValue
Original Post




Injecting a bean instance into another bean, programmatically.

package com.logicbig.example;

public class MyBean {
private MyOtherBean otherBean;

public void setOtherBean(MyOtherBean otherBean) {
this.otherBean = otherBean;
}

public void doSomething() {
otherBean.doSomething();
}
}

package com.logicbig.example;

public class MyOtherBean {

public void doSomething() {
System.out.println("from other bean ");
}
}

package com.logicbig.example;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;

public class InjectingOtherBeans {

public static void main(String[] args) {
DefaultListableBeanFactory context =
new DefaultListableBeanFactory();

//define and register MyOtherBean
GenericBeanDefinition beanOtherDef = new GenericBeanDefinition();
beanOtherDef.setBeanClass(MyOtherBean.class);
context.registerBeanDefinition("other", beanOtherDef);

//define and register myBean
GenericBeanDefinition beanDef = new GenericBeanDefinition();
beanDef.setBeanClass(MyBean.class);
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.addPropertyValue("otherBean", context.getBean("other"));
beanDef.setPropertyValues(mpv);
context.registerBeanDefinition("myBean", beanDef);

//using MyBean instance
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}

Output

from other bean 
Original Post




Programmatically registering beans by implementing BeanDefinitionRegistryPostProcessor

package com.logicbig.example;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;

public class MyBeanDefinitionRegistryPostProcessor
implements BeanDefinitionRegistryPostProcessor {

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
throws BeansException {

GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(MyBean.class);
bd.getPropertyValues().add("strProp", "my string property");
registry.registerBeanDefinition("myBeanName", bd);
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
//no op
}
}

package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
@Bean
public MyBeanDefinitionRegistryPostProcessor myConfigBean() {
return new MyBeanDefinitionRegistryPostProcessor();
}
}

package com.logicbig.example;

public class MyBean {
private String strProp;

public void setStrProp(String strProp) {
this.strProp = strProp;
}

public void doSomething() {
System.out.println("from MyBean: " + strProp);
}
}

package com.logicbig.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanDefinitionRegistryPostProcessorExample {

public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);

MyBean bean = (MyBean) context.getBean("myBeanName");
bean.doSomething();
}
}

Output

from MyBean:  my string property
Original Post

Programmatically registering beans by implementing BeanDefinitionRegistryPostProcessor

package com.logicbig.example;

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 MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

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

GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(MyBean.class);
bd.getPropertyValues().add("strProp", "my string property");

((DefaultListableBeanFactory) beanFactory)
.registerBeanDefinition("myBeanName", bd);
}
}

package com.logicbig.example;

public class MyBean {
private String strProp;

public void setStrProp(String strProp) {
this.strProp = strProp;
}

public void doSomething() {
System.out.println("from MyBean: " + strProp);
}
}

package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
@Bean
public static MyBeanFactoryPostProcessor myConfigBean() {
return new MyBeanFactoryPostProcessor();
}
}

package com.logicbig.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanFactoryPostProcessorExample {

public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}

}

Output

from MyBean:  my string property
Original Post




See Also