setter注入集合属性(了解)
Spring属性注入还支持集合对象的使用,常见的集 合对象注入如下
List/数组
Set
Map
Properties
eg:
/** * 实体类 * @author Thinkpad * */ public class User { private List<String> list; private String[] arr; private Set<String> set; private Map<String,String> map; private Properties property; private String name; private int age; public User(){} public User(List<String> list, String[] arr, Set<String> set, Map<String, String> map, Properties property, String name, int age) { super(); this.list = list; this.arr = arr; this.set = set; this.map = map; this.property = property; this.name = name; this.age = age; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public String[] getArr() { return arr; } public void setArr(String[] arr) { this.arr = arr; } 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 getProperty() { return property; } public void setProperty(Properties property) { this.property = property; } 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; } } <!-- 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="user" class="com.wd.bean.User"> <property name="name" value="王五" /> <property name="age" value="33"/> <property name="list"> <list> <value>1</value> <value>2</value> <value>3</value> <value>4</value> <value>5</value> </list> </property> <property name="arr"> <array> <value type="java.lang.String">beijing</value> <value type="java.lang.String">shanghai</value> <value type="java.lang.String">tianjing</value> </array> </property> <property name="set"> <set> <value>aaa</value> <value>bbb</value> <value>ccc</value> </set> </property> <property name="map"> <map> <entry key="1" value="姓名"/> <entry key="2" value="性别"/> <entry key="3" value="年龄"/> </map> </property> <property name="property"> <props> <prop key="ww">ww...</prop> <prop key="ww1">ww1...</prop> <prop key="ww2">ww2...</prop> </props> </property> </bean> </beans> /** * 测试类 * @author Thinkpad * */ public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) ac.getBean("user"); System.out.println("List集合:"+user.getList()); System.out.println("Set集合:"+user.getSet()); String[] s = user.getArr(); for(String L:s){ System.out.println("arry数组:"+L); } Map<String,String> map = user.getMap(); for(Map.Entry<String, String> m:map.entrySet()){ System.out.println("map集合:key:"+m.getKey()+"value:"+m.getValue()); } Properties pro = user.getProperty(); Set<Entry<Object, Object>> entrys = pro.entrySet();//返回的属性键值对实体 for(Entry<Object, Object> entry:entrys){ System.out.println("Properties属性:key:"+entry.getKey()+",value:"+entry.getValue()); } } }
输出结果: