1.定义一个实体,包含这五种“集合”的私有变量
public class Pojo {
private Object[] str;
private List list;
private Set set;
private Map map;
private Properties properties;
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Pojo{");
sb.append("str=").append(str == null ? "null" : Arrays.asList(str).toString());
sb.append(", list=").append(list);
sb.append(", set=").append(set);
sb.append(", map=").append(map);
sb.append(", properties=").append(properties);
sb.append('}');
return sb.toString();
}
public Object[] getStr() {
return str;
}
public void setStr(Object[] str) {
this.str = str;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
此实体类提供包括属性的setter和getter方法,目地为了给属性赋值
还有一个重写父类的toString方法,打印出每个属性的值。
2.创建一个Student实体类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Student{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append('}');
return sb.toString();
}
}
此类作为一个引用数据类型,为了比较和基本类型在注入时的区别
3.数组注入的方式
首先在maven项目中src/main/java/resources下创建一个xml文件
下面就是关于数组注入的方式
<bean id="stu" class="com.qc.IOC.Student">
<property name="age" value="18"/>
<property name="name" value="xiaohong"/>
</bean>
<bean id="pojo" class="com.qc.IOC.Pojo">
<property name="str">
<array>
<value>zhubajie</value>
<value>11122</value>
<ref bean="stu"/>
</array>
</property>
</bean>
第一个bean标签通过标准的setter方法注入,bean标签中的id是唯一标识,class的值是你要注入的实体类,property标签里name表示的是属性名(基本数据类型),value表示你要赋给这个属性的值。
第二个bean标签是对数组类型的注入,不同于第一种的是:array标签表示为数组类型的属性进行定义值,因为数组里的值可以为多个,因此在array标签下的value标签有多个,其中ref标签表示为引用数据类型进行定义值,bean的值为第一个bean标签的id值(Object类型的数组里面可以存字符串、包装类、引用类型等)。
4.list集合的注入
<bean id="pojo" class="com.qc.IOC.Pojo">
<property name="list">
<list>
<ref bean="stu"/>
<value>xiaoli</value>
<value>xiaoli</value> <!--不去重-->
<value>1000</value>
</list>
</property>
</bean>
list标签表示为list集合的属性进行定义值,子标签中的value标签表示基本的类型的值而ref标签为对象类型的值,对于相同的值不会进行去重,因为list集合是不去重的。由于list集合中的值为引用数据类型所有对于int类型的值会自动装箱为Integer类型
5.set集合的注入
<bean id="pojo" class="com.qc.IOC.Pojo">
<property name="set">
<set>
<value>xiaohong</value>
<value>xiaohong</value> <!--去重-->
<value>2000</value>
<value>null</value>
<ref bean="stu"/>
</set>
</property>
</bean>
和list集合差不多,需要用set标签来定义值。并且相同的value值会去重,也可以定义值为null。
6.map集合的注入
<bean id="pojo" class="com.qc.IOC.Pojo">
<property name="map">
<map>
<entry key="小李" value="18"/>
<entry key="小李" value="20"/>
<entry key="null" value="null"/>
<entry key="小红" value-ref="stu"/>
</map>
</property>
</bean>
因为map集合是一个键值对的形式来存储的,所以在map标签的子标签entry中有key和value的值分别对应的是map集合中的键和值。
7.properties的注入
<bean id="pojo" class="com.qc.IOC.Pojo">
<property name="properties">
<props>
<prop key="username">小张</prop>
<prop key="password">11111</prop>
</props>
</property>
</bean>
Properties属性的值读取的时候是以键值对的形式读取,注意:在prop子标签下的值是写在尖括号外面。
8.创建测试类
public class TestIOC {
@Test
public void testArray() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Pojo bean = (Pojo) ac.getBean("pojo");
System.out.println(bean);
}
}
9运行结果
Pojo{str=[zhubajie, 11122, Student{name='xiaohong', age=18}], list=[Student{name='xiaohong', age=18}, xiaoli, xiaoli, 1000], set=[xiaohong, 2000, null, Student{name='xiaohong', age=18}], map={小李=20, null=null, 小红=Student{name='xiaohong', age=18}}, properties={password=11111, username=小张}}