持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第15天,点击查看活动详情
不同类型属性赋值
为类类型属性赋值
加一个Clazz类
package com.sentiment.pojo;
public class Clazz {
private Integer cid;
private String cname;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Clazz{" +
"cid=" + cid +
", cname='" + cname + ''' +
'}';
}
}
并在student类中,添加一个Clazz类型的属性
private Clazz clazz;
外部bean
ref:引用IOC容器中的某个bean的id
Clazz是类对象,因此不能直接使用value赋值,要使用ref
<bean id="studentFive" class="com.sentiment.pojo.Student">
<property name="sid" value="1845"></property>
<property name="sname" value="Demo"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
<property name="clazz" ref="clazzOne"></property>
</bean>
<bean id="clazzOne" class="com.sentiment.pojo.Clazz">
<property name="cid" value="1"></property>
<property name="cname" value="QLNU"></property>
</bean>
测试
@Test
public void testByRef(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
Student studentFive = ioc.getBean("studentFive", Student.class);
System.out.println(studentFive);
}
级联方式
这种方式,仍需用ref,因为不是用的话 clazz值为空,匹配不到对应的cid和cname,所以这种方式也就相当于一种另外的赋值方式
<bean>
<property name="clazz" ref="clazzOne"></property>
<property name="clazz.cid" value="2"></property>
<property name="clazz.cname" value="QlNU2"></property>
</bean>
<bean id="clazzOne" class="com.sentiment.pojo.Clazz">
<property name="cid" value="1"></property>
<property name="cname" value="QLNU"></property>
</bean>
内部bean
内部bean,只能在当前bean的内部使用,不能直接通过ioc容器得到
<bean id="studentFive" class="com.sentiment.pojo.Student">
<property name="sid" value="1845"></property>
<property name="sname" value="Demo"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
<property name="clazz">
<bean id="clazzInner" class="com.sentiment.pojo.Clazz">
<property name="cid" value="2"></property>
<property name="cname" value="QLNU2"></property>
</bean>
</property>
<!-- <property name="clazz.cid" value="2"></property>-->
<!-- <property name="clazz.cname" value="QlNU2"></property>-->
</bean>
数组类型属性赋值
添加一个字符串数组型变量 hobby,并设置对应的setter、getter和toString方法
private String[] hobby;
数组类型赋值有对应的标签<array>,如果数组存储的是类变量,则将value标签改为ref标签
<property name="hobby">
<array>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</array>
</property>
list集合类型赋值
在clazz类中添加一个students集合属性,并设置对应的setter、getter和toString方法
private List<Student> students;
list标签
同数组形式,直接用list标签即可,由于存储的事Student类型数据,所以里边用ref标签
<property name="students">
<list>
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
</list>
</property>
util标签
list标签相当于是内部调用,而util就相当于外部调用
<!--通过ref调用util标签-->
<property name="students" ref="studentList"></property>
<!--定义一个util标签-->
<util:list id="studentList">
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
</util:list>
map集合类型赋值
新建一个Teacher类
package com.sentiment.pojo;
public class Teacher {
private Integer tid;
private String tname;
@Override
public String toString() {
return "Teacher{" +
"tid=" + tid +
", tname='" + tname + ''' +
'}';
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
在Student类中,定义一个 teacher类型的map属性,并设置对应的setter、getter、toString方法
private Map<String,Teacher> teacherMap;
map标签
map中的entry标签 ,自动存储键和值,设置value-ref存储 bean中的类型数据
<property name="teacherMap">
<map>
<entry key="1" value-ref="teacherOne"></entry>
<entry key="2" value-ref="teacherTwo"></entry>
</map>
</property>
<bean id="teacherOne" class="com.sentiment.pojo.Teacher">
<property name="tid" value="1"></property>
<property name="tname" value="AA"></property>
</bean>
<bean id="teacherTwo" class="com.sentiment.pojo.Teacher">
<property name="tid" value="2"></property>
<property name="tname" value="BB"></property>
</bean>
util标签
<!--调用定义的map类型的util标签--->
<property name="teacherMap" ref="studentMap"></property>
<!--定义map类型的util标签-->
<util:map id="studentMap">
<entry key="1" value-ref="teacherOne"></entry>
<entry key="2" value-ref="teacherTwo"></entry>
</util:map>