---------------------------------/SpringHello2/src/ref/HelloBean.java
package ref;
public class HelloBean {
private String hello;
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}
---------------------------------/SpringHello2/src/ref/HelloDate.java
package ref;
import java.util.Date;
public class HelloDate {
private Date date;
private HelloBean hb;
public void setDate(Date date) {
this.date = date;
}
public void setHb(HelloBean hb) {
this.hb = hb;
}
public void sayHello() {
System.out.println(hb.getHello() + " " + date.toLocaleString());
}
}
---------------------------------/SpringHello2/src/ref/Test.java
package ref;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Test {
public static void main(String[] args) {
BeanFactory parent = new XmlBeanFactory(new ClassPathResource(
"ref/parent.xml"));
BeanFactory child = new XmlBeanFactory(new ClassPathResource(
"ref/beans.xml"), parent);
HelloDate hd1 = (HelloDate) child.getBean("hd1");
HelloDate hd2 = (HelloDate) child.getBean("hd2");
HelloDate hd3 = (HelloDate) child.getBean("hd3");
hd1.sayHello();
hd2.sayHello();
hd3.sayHello();
}
}
---------------------------------/SpringHello2/src/ref/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="ref.HelloBean">
<property name="hello" value="Hello! Child Bean." />
</bean>
<bean id="dateBean" name="#date" class="java.util.Date" />
<bean id="hd1" class="ref.HelloDate">
<property name="hb">
<ref bean="helloBeanParent" />
</property>
<property name="date">
<ref bean="#date" />
</property>
</bean>
<bean id="hd2" class="ref.HelloDate">
<property name="hb">
<ref local="helloBean" />
</property>
<property name="date">
<ref local="dateBean" />
</property>
</bean>
<bean id="hd3" class="ref.HelloDate">
<property name="hb">
<ref parent="helloBean" />
</property>
<property name="date">
<bean class="java.util.Date" />
</property>
</bean>
</beans>
---------------------------------/SpringHello2/src/ref/parent.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="ref.HelloBean">
<property name="hello" value="Hello! ZYLJava." />
</bean>
<bean id="helloBeanParent" class="ref.HelloBean">
<property name="hello" value="Hello! Parent Bean." />
</bean>
</beans>