读书人

施用编码方式创建和绑定Bean

发布时间: 2012-07-01 13:15:00 作者: rapoo

使用编码方式创建和绑定Bean

?

//创建Bean工厂DefaultListableBeanFactory factory = new DefaultListableBeanFactory();//生成BeanDefinitionAbstractBeanDefinition newA = new RootBeanDefinition(A.class);newA.setScope(BeanDefinition.SCOPE_SINGLETON);AbstractBeanDefinition newB = new RootBeanDefinition(B.class);newB.setScope(BeanDefinition.SCOPE_PROTOTYPE);//注册factory.registerBeanDefinition("a", newA);factory.registerBeanDefinition("b", newB);//功过构造函数注入ConstructorArgumentValues argValues = new ConstructorArgumentValues();argValues.addIndexedArgumentValue(0, newB);newA.setConstructorArgumentValues(argValues);//通过Setter方式注入MutablePropertyValues propertyValues = new MutablePropertyValues();propertyValues.addPropertyValue(new PropertyValue("b", newB));newA.setPropertyValues(propertyValues);A a = (A) factory.getBean("a");a.getB().output();

?

?

?

?

?

?

?

?

读书人网 >软件架构设计

热点推荐