五种“集合”的注入
1Array:可以重复
<property name="arrays">
<array>
<value>java</value>
<value>java</value><!--允许重复-->
<value>html5</value>
<value>python</value>
<value>testing</value>
</array>
</property>
2List:有序可重复
<property name="list">
<list>
<value>zhouxingxing</value>
<value>zhouxingxing</value><!--允许重复-->
<value>9527</value>
<ref bean="obj" />
<ref bean="stu" />
</list>
</property>
3Set:无序不可重复
<property name="set">
<set>
<value>suwukong</value>
<value>suwukong</value> <!--该值不会被加入,已经存在-->
<value>zhubajie</value>
<value>tangtang</value>
<value>shaheshang</value>
</set>
</property>
4.Map null, “”, " ", " ", “null”
<property name="map">
<map>
<entry key="jack" value="杰克"/> <!--添加方法返回为null-->
<entry key="jack" value="杰克2"/> <!--添加方法返回为杰克-->
<entry key="rose" value="肉丝"/> <!--添加方法返回为null-->
<entry key="rose" value="null"/> <!--添加方法返回为肉丝,map里允许空value-->
<entry key="null" value="肉丝"/> <!--map允许空key-->
<entry key="null" value="null"/> <!--map里允许key和value同时为null,该方法返回肉丝-->
</map>
</property>
5.Propertis
<property name="prop">
<props>
<prop key="url">jdbc:mysql://localhost:3306/hello</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
完整代码 MyColl.java
package com.qfedu.ioc;
import java.util.*;
public class MyColl {
private String[] arrays;
private List<Object> list;
private Set<String> set;
private Map<String, String> map;
private Properties prop;
@Override
public String toString() {
return "MyColl{" +
"arrays=" + Arrays.toString(arrays) +
", list=" + list +
", set=" + set +
", map=" + map +
", prop=" + prop +
'}';
}
public String[] getArrays() {
return arrays;
}
public void setArrays(String[] arrays) {
this.arrays = arrays;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
ioc.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="obj" class="java.lang.Object" />
<bean id="stu" class="com.qfedu.pojo.Student">
<property name="sid" value="08" />
<property name="name" value="zhangsan" />
<property name="sex" value="true" />
<property name="score" value="100" />
<property name="age" value="18" />
</bean>
<bean id="myColl" class="com.qfedu.ioc.MyColl">
<property name="arrays">
<array>
<value>java</value>
<value>java</value><!--允许重复-->
<value>html5</value>
<value>python</value>
<value>testing</value>
</array>
</property>
<property name="list">
<list>
<value>zhouxingxing</value>
<value>zhouxingxing</value><!--允许重复-->
<value>9527</value>
<ref bean="obj" />
<ref bean="stu" />
</list>
</property>
<property name="set">
<set>
<value>suwukong</value>
<value>suwukong</value> <!--该值不会被加入,已经存在-->
<value>zhubajie</value>
<value>tangtang</value>
<value>shaheshang</value>
</set>
</property>
<property name="map">
<map>
<entry key="jack" value="杰克"/> <!--添加方法返回为null-->
<entry key="jack" value="杰克2"/> <!--添加方法返回为杰克-->
<entry key="rose" value="肉丝"/> <!--添加方法返回为null-->
<entry key="rose" value="null"/> <!--添加方法返回为肉丝,map里允许空value-->
<entry key="null" value="肉丝"/> <!--map允许空key-->
<entry key="null" value="null"/> <!--map里允许key和value同时为null,该方法返回肉丝-->
</map>
</property>
<property name="prop">
<props>
<prop key="url">jdbc:mysql://localhost:3306/hello</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
TestColl.java
package com.qfedu.ioc;
import org.junit.Test;
import java.util.*;
public class TestColl {
@Test
public void test(){
Map<String, String> map = new HashMap<>();
System.out.println(map.put("jack", "杰克"));
System.out.println(map.put("rose", "肉丝"));
System.out.println(map.put("tom", "汤姆"));
System.out.println(map.put(null, "汤姆"));
System.out.println(map.put("jerry", "杰瑞"));
System.out.println(map.put("jerry", null));
System.out.println(map.put(null, null));
System.out.println(map);
}
@Test
public void testGetRandomList(){
List<Integer> list = new ArrayList<>();
Set<Integer> set = new HashSet<>();
while (list.size()< 10){
int num = (int)(Math.random() * 10);
if(set.add(num)){
list.add(num);
}
}
System.out.println(set);
System.out.println(list);
}
}
TestCollection.java
package com.qfedu.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCollection {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/qfedu/ioc/ioc.xml");
MyColl mc = ac.getBean("myColl", MyColl.class);
System.out.println(mc);
}
}