Java 如何以编程方式创建具有注入属性的 bean 定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3370161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically create bean definition with injected properties?
提问by Fixpoint
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?
我想以编程方式将 bean 定义添加到应用程序上下文,但该定义的某些属性是来自该上下文的其他 bean(我知道它们的名称)。我该怎么做才能注入这些属性?
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.
我正在使用 Spring 3.0。
采纳答案by axtavt
Use RuntimeBeanReference
:
使用RuntimeBeanReference
:
values.addPropertyValue("beanProperty", new RuntimeBeanReference("beanName"));
回答by Bozho
You can:
你可以:
- using the javaconfigproject.
- using
BeanDefinitionParser
- but this is a bit ugly to write.
- 使用javaconfig项目。
- 使用
BeanDefinitionParser
- 但这写起来有点难看。
回答by Fixpoint
I found the solution. I have to use another BeanDefinition
as a property, like this:
我找到了解决方案。我必须使用另一个BeanDefinition
作为属性,如下所示:
GenericBeanDefinition bd2 = new GenericBeanDefinition();
bd2.setBeanClass(Dependency.class);
GenericBeanDefinition bd1 = new GenericBeanDefinition();
bd1.setBeanClass(Component.class);
MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue("dependency", bd2);
bd1.setPropertyValues(values);
回答by Sean Patrick Floyd
I would add a bean like this that has access to the applicationContext:
我会添加一个像这样可以访问 applicationContext 的 bean:
public class AppContextExtendingBean implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
// do it like this
version1(beanFactory);
// or like this
version2(beanFactory);
}
// let spring create a new bean and then manipulate it (works only for singleton beans, obviously)
private void version1(AutowireCapableBeanFactory beanFactory){
MyObject newBean = (MyObject) beanFactory.createBean(MyObject.class,AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
newBean.setBar("baz");
newBean.setFoo("foo");
newBean.setPhleem("phleem");
beanFactory.initializeBean(newBean, "bean1");
}
// create the object manually and then inject it into the spring context
private void version2(AutowireCapableBeanFactory beanFactory){
MyObject myObject=new MyObject("foo","phleem");
myObject.setBar("baz");
beanFactory.autowireBean(myObject);
beanFactory.initializeBean(myObject, "bean2");
}
}