准备:构造如下两个类
public class Dept {
private String dName;
private List<Emp> emps;
public void setdName(String dName) {
this.dName = dName;
}
public void setEmps(List<Emp> emps) {
this.emps = emps;
}
public String getdName() {
return dName;
}
public List<Emp> getEmps() {
return emps;
}
@Override
public String toString() {
return "Dept{" +
"dName='" + dName + '\'' +
", emp=" + emps +
'}';
}
}
---
package com.study.spring6.iocxml.deptAndEmp;
public class Emp {
private String name;
private String age;
private Dept dept;
public void work(){
System.out.println("emp is working...and his dept is:");
System.out.println(dept);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
",dept=" + dept +
'}';
}
}
方式一:引用外部 bean
<bean id="dept" class="com.study.spring6.iocxml.deptAndEmp.Dept">
<property name="dName" value="security"/>
</bean>
<bean id="emp" class="com.study.spring6.iocxml.deptAndEmp.Emp">
<property name="name" value="lucy"/>
<property name="age" value="50"/>
<property name="dept" ref="dept"/>
</bean>
可以看到 emp 中通过 ref
属性注入了 Dept 对象,当然这个做法的前提是配置了 Dept 对象对应的 bean
测试类
package com.study.spring6.iocxml.deptAndEmp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestEmp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Emp emp = (Emp) context.getBean("emp");
System.out.println(emp);
emp.work();
}
}
执行结果
Emp{name='lucy', age='50',dept=Dept{dName='security', emp=null}}
emp is working...and his dept is:
Dept{dName='security', emp=null}
方式二:使用内部 bean
配置
在某一个 property 内部再声明一个 bean 就是内部 bean
注意:内部 bean 只能用于给属性赋值,不能在外部通过 IoC 容器获取,因此可以省略 id 属性
<bean id="emp" class="com.study.spring6.iocxml.deptAndEmp.Emp">
<property name="name" value="mary"/>
<property name="age" value="20"/>
<property name="dept">
<bean class="com.study.spring6.iocxml.deptAndEmp.Dept">
<property name="dName" value="money"/>
</bean>
</property>
</bean>
执行结果
Emp{name='mary', age='20',dept=Dept{dName='money', emp=null}}
emp is working...and his dept is:
Dept{dName='money', emp=null}
方式三:级联属性赋值
使用的较少
特殊类型注入-数组与集合
数组
给 Emp 添加上属性 private String[] love;
表示员工爱好
配置
<bean id="dept" class="com.study.spring6.iocxml.deptAndEmp.Dept">
<property name="dName" value="IT"/>
</bean>
<bean id="emp" class="com.study.spring6.iocxml.deptAndEmp.Emp">
<property name="name" value="mary"/>
<property name="age" value="20"/>
<property name="dept" ref="dept"/>
<property name="love">
<array>
<value>跑步</value>
<value>睡觉</value>
<value>学习</value>
</array>
</property>
</bean>
执行结果
Emp{name='mary', age='20', dept=Dept{dName='IT', emp=null}, love=[跑步, 睡觉, 学习]}
标签:String,自定义,Dept,dName,dept,emp,类型,public,注入
From: https://www.cnblogs.com/ShaunY/p/17556010.html