使用编码方式创建和绑定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();?
?
?
?
?
?
?
?