BeanFactory分層
package org.springframework.beans.factory;//分層工廠
public interface HierarchicalBeanFactory extends BeanFactory {//返回工廠的父工廠BeanFactory getParentBeanFactory();//這個工廠中是否包含這個Beanboolean containsLocalBean(String name);
}
測試代碼
@Testpublic void testAutowireWithParent() throws Exception {DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(AUTOWIRE_CONTEXT);DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();MutablePropertyValues pvs = new MutablePropertyValues();pvs.add("name", "kerry");RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);bd.setPropertyValues(pvs);lbf.registerBeanDefinition("spouse", bd);xbf.setParentBeanFactory(lbf);doTestAutowire(xbf);}private void doTestAutowire(DefaultListableBeanFactory xbf) throws Exception {DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");TestBean kerry = (TestBean) xbf.getBean("spouse");// should have been autowiredassertEquals(kerry, rod1.getSpouse());}
調用了setParentBeanFactory設置BeanFactory,lbf中注冊了spouse,所以在調用getBean時可以獲取到spouse