Emp.java
test
package com.lovo.bean;
public class Emp {
private String name;
private Dept dept;
public Emp() {
super();
}
public Emp(String name, Dept dept) {
super();
this.name = name;
this.dept = dept;
}
public String getName() {
return name;
}
public Dept getDept() {
return dept;
}
}
Dept.java
package com.lovo.bean;
public class Dept {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
bean.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="dept" class="com.lovo.bean.Dept">
<property name="name" value="员工"></property>
</bean>
<bean id="emp" class="com.lovo.bean.Emp">
<constructor-arg value="财务部" />
<constructor-arg ref="dept" />
</bean>
</beans>
test
package com.lovo.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lovo.bean.Emp;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Emp emp = (Emp) ac.getBean("emp");
System.out.println(emp.getDept().getName());
}
}