package com.h3.common;
public class customer
{
private person person;
public customer(person person) {
this.person = person;
}
public void setperson(person person) {
this.person = person;
}
@override
public string tostring() {
return "customer [person=" + person + "]";
}
}
package com.h3.common;
public class person
{
private string name;
private string address;
private int age;
//getter and setter methods
@override
public string tostring() {
return "person [address=" + address + ",
age=" + age + ", name=" + name + "]";
}
}
<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-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <property name="person" ref="personbean" /> </bean> <bean id="personbean" class="com.h3.common.person"> <property name="name" value="h3" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </beans>
<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-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <property name="person"> <bean class="com.h3.common.person"> <property name="name" value="h3" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </property> </bean> </beans>
<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-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <constructor-arg> <bean class="com.h3.common.person"> <property name="name" value="h3" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </constructor-arg> </bean> </beans>
执行结果:
package com.h3.common;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class app
{
public static void main( string[] args )
{
applicationcontext context =
new classpathxmlapplicationcontext(new string[] {"spring-customer.xml"});
customer cust = (customer)context.getbean("customerbean");
system.out.println(cust);
}
}
输出结果:
customer [person=person [address=address1, age=28, name=h3]]