spring-04
2018-11-05 21:39 crow! 阅读(170) 评论(0) 编辑 收藏 举报集合注入
Member
1 public class Member { 2 private List<String> names ; 3 private Set<String> emails ; 4 private Map<String,String> tels ; 5 private Properties infos ; 6 7 public Properties getInfos() { 8 return infos; 9 } 10 11 public void setInfos(Properties infos) { 12 this.infos = infos; 13 } 14 15 public List<String> getNames() { 16 return names; 17 } 18 19 public void setNames(List<String> names) { 20 this.names = names; 21 } 22 23 public Set<String> getEmails() { 24 return emails; 25 } 26 27 public void setEmails(Set<String> emails) { 28 this.emails = emails; 29 } 30 31 public Map<String, String> getTels() { 32 return tels; 33 } 34 35 public void setTels(Map<String, String> tels) { 36 this.tels = tels; 37 } 38 39 @Override 40 public String toString() { 41 return "Member [names=" + names + ", emails=" + emails + ", tels=" + tels + ", infos=" + infos + "]"; 42 } 43 }
applicationContext.xml
在默认情况下会用ArrayList为List接口实例化。
LinkedHashSet实例化Set 。结合了List的顺序保存,和Set的不容许重复。
<bean id="member" class="cn.mldn.vo.Member" >
<property name="names" >
<!-- 这里不写value-type 也可以正确为集合设置内容 -->
<list value-type="java.lang.String">
<value>Smith</value>
<value>Tony</value>
<value>Smith</value>
</list>
</property>
<property name="emails">
<set>
<value>test_123@qq.com</value>
<value>xiaohuang@163.com</value>
<value>bupbi@qq.com</value>
<value>bupbi@qq.com</value>
</set>
</property>
<property name="tels">
<map>
<entry key="110" value="公安" />
<entry>
<key><value>120</value></key>
<value>急救</value>
</entry>
</map>
</property>
<property name="infos">
<props>
<prop key="mldn">www.mldn.cn</prop>
<prop key="asdf">asdfasdfas</prop>
</props>
</property>
</bean>
内部bean模式
<bean id="emp" class="cn.mldn.vo.Emp"> <property name="empno" value="7369"/> <!-- 如果不想设置内容可以写null标签或不写property --> <property name="ename" ><null/></property> <property name="retire" value="off" /> <property name="dept" > <!-- 内部bean模式 --> <bean id="d" class="cn.mldn.vo.Dept" > <property name="deptno" value="101" /> <property name="dname" value="市场部" /> <property name="loc" value="上海"/> </bean> </property> </bean>
P命名空间
public class Emp implements Serializable { private Integer empno ; private String ename ; private Dept dept ; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", dept=" + dept + "]"; } }
package cn.mldn.vo; import java.beans.ConstructorProperties; import java.io.Serializable; @SuppressWarnings("serial") public class Dept implements Serializable { private Integer deptno ; private String dname ; private String loc ; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } @Override public String toString() { return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; } }
<bean id="dept" class="cn.mldn.vo.Dept" p:deptno="10" p:dname="开发部" p:loc="北京" /> <bean id="emp" class="cn.mldn.vo.Emp" p:empno="7369" p:ename="Smith" p:dept-ref="dept" />