有两个类,部门和员工。部门和员工的对应关系是一对多(1:m)。
部门类Dept:
package com.kiong.spring6;
public class Dept {
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public void printInfo(){
System.out.println("部门名称是:"+deptName);
}
}
员工类Employee类:
package com.kiong.spring6;
public class Employee {
private String name;
private int age;
Dept dpt;
public Employee() {
}
public Employee(String name, int age, Dept dpt) {
this.name = name;
this.age = age;
this.dpt = dpt;
}
public void work(){
System.out.println("员工"+name+"的年龄是"+age);
dpt.printInfo();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dept getDpt() {
return dpt;
}
public void setDpt(Dept dpt) {
this.dpt = dpt;
}
}
方式1:引用外部bean。xml文件beanEmp.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">
<!--方式1,引用外部bean
1 创建两个对象,dept和emp
2 在emp的bean标签里面,使用property引入dept的bean
-->
<bean id="dept" class="com.kiong.spring6.Dept">
<property name="deptName" value="安保部"></property>
</bean>
<bean id="emp" class="com.kiong.spring6.Employee">
<!--普通属性注入-->
<property name="name" value="Kiong"></property>
<property name="age" value="21"></property>
<!--注入对象类型属性
private Dept dpt;
-->
<property name="dpt" ref="dept"></property>
</bean>
</beans>
测试类TestEmp:
package com.kiong.spring6;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestEmp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beanEmp.xml");
Employee emp = context.getBean("emp",Employee.class);
emp.work();
}
}
方式1运行结果如下:
方式2:引用内部bean。xml文件beanEmp.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">
<!--方式2,内部bean
-->
<bean id="emp" class="com.kiong.spring6.Employee">
<!--普通属性注入-->
<property name="name" value="Lisa"></property>
<property name="age" value="20"></property>
<!--内部bean注入-->
<property name="dpt">
<bean id="dept" class="com.kiong.spring6.Dept">
<property name="deptName" value="人力部"></property>
</bean>
</property>
</bean>
</beans>
方式2运行结果如下:
方式3:级联注入。xml文件beanEmp.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">
<!--方式3,级联属性注入
-->
<bean id="dept" class="com.kiong.spring6.Dept">
<property name="deptName" value="人力部"></property>
</bean>
<bean id="emp" class="com.kiong.spring6.Employee">
<!--普通属性注入-->
<property name="name" value="David"></property>
<property name="age" value="21"></property>
<!--级联注入-->
<property name="dpt" ref="dept"></property>
<property name="dpt.deptName" value="技术研发部"></property>
</bean>
</beans>
方式3运行结果如下: