动态创建Spring Bean
GenericApplicationContext ctx = new GenericApplicationContext();
BeanDefinitionBuilder builderA = BeanDefinitionBuilder
.rootBeanDefinition(BeanA.class)
.addPropertyValue("name", "Joe");
ctx.registerBeanDefinition("bean-a", builderA.getBeanDefinition());
BeanDefinitionBuilder builderB = BeanDefinitionBuilder.rootBeanDefinition(BeanB.class)
.addPropertyReference("refA", "bean-a");
ctx.registerBeanDefinition("bean-b", builderB.getBeanDefinition());
ctx.getBean("bean-b");
上面的例子动态创建了两个 bean 定义,第二个Bean 引用了第一个Bean。