注入和BeanFactory嵌套
首先是子BeanFactory:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="name" ref="name"/> <property name="age" value="2"/> <property name="height" value="0.8"/> <property name="isProgrammer" value="false"/> <property name="ageInSeconds" value="63072000"/> </bean> <bean id="injectSimpleChild2" value="2"/> <property name="height" value="0.8"/> <property name="isProgrammer" value="false"/> <property name="ageInSeconds" value="63072000"/> </bean></beans>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="name" ref="name"/> <property name="age" value="2"/> <property name="height" value="0.8"/> <property name="isProgrammer" value="false"/> <property name="ageInSeconds" value="63072000"/> </bean></beans>
package com.apress.prospring2.ch03.beanfactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;/** * @author janm */public class HierarchicalBeanFactoryDemo { public static void main(String[] args) { XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("/META-INF/spring/injectdemo2-context.xml")); XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("/META-INF/spring/injectdemo3-context.xml"), parent); System.out.println(parent.getBean("injectSimpleParent")); System.out.println(child.getBean("injectSimpleChild")); System.out.println(child.getBean("injectSimpleChild2")); }}Name: John SmithAge: 2Age in Seconds: 63072000Height: 0.800000Is Programmer?: falseName: Johnny SmithAge: 2Age in Seconds: 63072000Height: 0.800000Is Programmer?: falseName: John SmithAge: 2Age in Seconds: 63072000Height: 0.800000Is Programmer?: false