GenericBeanDefinition is an implementation of BeanDefinition. This example adds a new bean definition to bean factory.
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();
}
private static class MyBean {
private Date date;
public void doSomething () {
System.out.println("from my bean, date: " + date);
}
public void setDate (Date date) {
this.date = date;
}
}
}
Output
from my bean, date: Wed May 17 12:20:58 CDT 2017
Original PostThis example programmatically adds a BeanDefinition to bean factory and at the same time injects it's property which is another 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);
//definine 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();
}
private static class MyBean {
private MyOtherBean otherBean;
public void setOtherBean (MyOtherBean otherBean) {
this.otherBean = otherBean;
}
public void doSomething () {
otherBean.doSomething();
}
}
private static class MyOtherBean {
public void doSomething () {
System.out.println("from other bean ");
}
}
}
Output
from other bean
Original PostAnother way to inject properties.
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
public class InjectingOtherBeans2 {
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);
beanDef.getPropertyValues().
addPropertyValue("otherBean", context.getBean("other"));
context.registerBeanDefinition("myBean", beanDef);
//using MyBean instance
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
private static class MyBean {
private MyOtherBean otherBean;
public void setOtherBean (MyOtherBean otherBean) {
this.otherBean = otherBean;
}
public void doSomething () {
otherBean.doSomething();
}
}
private static class MyOtherBean {
public void doSomething () {
System.out.println("from other bean ");
}
}
}
Output
from other bean
Original Post