如何以编程方式创建具有注入属性的 bean 定义? [英] How to programmatically create bean definition with injected properties?
本文介绍了如何以编程方式创建具有注入属性的 bean 定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式将 bean 定义添加到应用程序上下文,但该定义的某些属性是来自该上下文的其他 bean(我知道它们的名称).我该怎么做才能注入这些属性?
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?
例如:
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);
我使用的是 Spring 3.0.
I'm using Spring 3.0.
推荐答案
使用RuntimeBeanReference
:
values.addPropertyValue("beanProperty", new RuntimeBeanReference("beanName"));
这篇关于如何以编程方式创建具有注入属性的 bean 定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文