Person.java:
package com.kinsey.woo.dto;
public class Person {
private Son son;
private String Id;
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public Person() {
super();
}
@Override
public String toString() {
return "Id: " + getId() + " " + getSon();
}
}
Son.java
package com.kinsey.woo.dto;
public class Son {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Son() {
super();
}
@Override
public String toString() {
return "name: " + getName() + " age : " + getAge();
}
}
RunMain.java
package com.kinsey.woo.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kinsey.woo.dto.Person;
import com.kinsey.woo.dto.Son;
public class RunMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-conf.xml");
Person person = ctx.getBean("person", Person.class);
System.out.println("Person:" + person);
Son son = ctx.getBean("son1", Son.class);
System.out.println("\n"+"son:" + son);
String name = ctx.getBean("theName1", String.class);
System.out.println("\n" + "Name: " + name);
}
}
spring-conf.xml
<?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="person" class="com.kinsey.woo.dto.Person">
<property name="id" value="No.1001"></property>
<property name="son">
<bean class="com.kinsey.woo.dto.Son">
<property name="age" value="22"></property>
<property name="name" value="David"></property>
</bean>
</property>
</bean>
<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value="person" />
<!-- 调用person.getSon()方法给son1 -->
<property name="propertyPath" value="son" />
</bean>
<bean id="theName1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value="son1"/>
<!-- 调用son1.getName()方法给theName1 -->
<property name="propertyPath" value="name"/>
</bean>
</beans>
结果: