如何通过注入属性以编程方式创建bean定义? [英] How to programmatically create bean definition with injected properties?
本文介绍了如何通过注入属性以编程方式创建bean定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
code> GenericBeanDefinition beanDef = new GenericBeanDefinition();
beanDef.setBeanClass(beanClass);
MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue(intProperty,10);
values.addPropertyValue(stringProperty,Hello,world);
values.addPropertyValue(beanProperty,/ *这里应该是什么?* /);
beanDef.setPropertyValues(values);
我使用的是Spring 3.0。
解决方案
使用 RuntimeBeanReference
:
values.addPropertyValue(beanProperty,新的RuntimeBeanReference(beanName));
I want to programmatically add a bean definition to an application context, but some properties of that definition are other beans from that context (I know their names). How can I do this so that those properties will be injected?
For example:
GenericBeanDefinition beanDef = new GenericBeanDefinition();
beanDef.setBeanClass(beanClass);
MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue("intProperty", 10);
values.addPropertyValue("stringProperty", "Hello, world");
values.addPropertyValue("beanProperty", /* What should be here? */);
beanDef.setPropertyValues(values);
I'm using Spring 3.0.
解决方案
Use RuntimeBeanReference
:
values.addPropertyValue("beanProperty", new RuntimeBeanReference("beanName"));
这篇关于如何通过注入属性以编程方式创建bean定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文