版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/huangbin10025/article/details/37049345
spring注入list 、set、 map、 properties
1.list
在xml中这样写:
<property name="list">
<list>
<value>Michael Huang</value>
<ref bean="student"></ref>
<value>110</value>
</list>
</property>
Java类中相同须要属性的setter和getter:
private List list;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
遍历一下,測试:
for (Object obj : list) {
System.out.println("看看你注入的这些是啥:" + obj);
}
console中打印注入的对象:
看看你注入的这些是啥:Michael Huang
看看你注入的这些是啥:<a target=_blank href="mailto:com.michael.spring.domain.student.Student@1d009b4">com.michael.spring.domain.student.Student@1d009b4</a>
看看你注入的这些是啥:110
ps:能够注入不同类型的对象,所以没有规定泛型接口,一切皆对象。什么都能够放。
2.set
<property name="set">
<set>
<value type="java.lang.String">Michael Jordon</value>
<ref bean="student"></ref>
</set>
</property>
3.map
<property name="map">
<map>
<entry key="abc" value="1231"></entry>
<entry key-ref="student" value-ref="student"></entry>
</map>
</property>
遍历map
Collection c = map.values();
Iterator it = c.iterator();
for (; it.hasNext();) {
System.out.println("from map---------" + it.next());
}
4properties
<property name="props">
<props>
<prop key="michael">876301469@qq.com</prop>
<prop key="tom">tom@163.com</prop>
</props>
</property>
Collection c1 = props.values();
Iterator it1 = c1.iterator();
for (; it1.hasNext();) {
System.out.println("from props---------" + it1.next());
}