Spring支持BeanFactory的嵌套使用,我们可以把一个大的配置文件拆分成多个配置文件,然后参考面向对象的继承机制,进行配置文件的“继承”,当嵌套bean工厂时,Spring允许其中的beans讲字工厂作为父工厂的参考bean,唯一缺点是这只能在配置文件中使用,调用子bean工厂的getBean来访问父工厂的一个bean是不可能的
嵌套BeanFacotry
realpathParent=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch4_BeanFactoryIterator"+File.separator+"applicationContext-parent.xml";
realpathChild=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch4_BeanFactoryIterator"+File.separator+"applicationContext-child.xml";
XmlBeanFactory parentFactory=new XmlBeanFactory(new FileSystemResource(realpathParent));
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpathChild),parentFactory);
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpathChild),parentFactory);
applicationContext-parent.xml

















applicationContext-child.xml



































SimpleTarget.java

















完整测试代码:









































运行结果
Bean in Parent
Bean in Child
Bean in Parent