如何通过注入属性以编程方式创建bean定义? [英] How to programmatically create bean definition with injected properties?

查看:149
本文介绍了如何通过注入属性以编程方式创建bean定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式将bean定义添加到应用程序上下文中,但该定义的某些属性是该上下文中的其他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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆