应以一个实体类,有Integer、Date、String、Map<>、List<>、Set<>、Url、File类型的属性
Map<>、List<>、Set<>这些属性注意要导 java.util 的包
package test;
import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
private Integer age;
private Date birthday;
private Integer sno;
private String username;
private Map<String,Integer> scores;
private List<String> lessons;
private URL url;
private Set<Integer> numbers;
private File photo;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Map<String, Integer> getScores() {
return scores;
}
public void setScores(Map<String, Integer> scores) {
this.scores = scores;
}
public List<String> getLessons() {
return lessons;
}
public void setLessons(List<String> lessons) {
this.lessons = lessons;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public Set<Integer> getNumbers() {
return numbers;
}
public void setNumbers(Set<Integer> numbers) {
this.numbers = numbers;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
@Override
public String toString() {
return "Student [age=" + age + ", birthday=" + birthday + ", sno=" + sno + ", username=" + username
+ ", scores=" + scores+", lessons=" + lessons.toString() + ", url=" + url + ", numbers=" + numbers.toString() + ", photo="
+ photo + "]";
}
}
配置bean.xml文件,在bean中给这些属性注入值
<?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="student" class="test.Student">
<!-- 给Integer的类型属性注入值 -->
<property name="age" value="13"></property>
<!-- 给Date的类型属性注入值 -->
<property name="birthday" value="2010/12/13"></property>
<!-- 给Integer的类型属性注入值 -->
<property name="sno" value="100"></property>
<!-- 给String的类型属性注入值 -->
<property name="username" value="hongtong"></property>
<!-- 给Map的类型属性注入值 -->
<property name="scores">
<map>
<entry key="yuwen" value="67"></entry>
<entry key="shuxue" value="97"></entry>
</map>
</property>
<!-- 给List的类型属性注入值 -->
<property name="lessons">
<list>
<value>maths</value>
<value>chinese</value>
</list>
</property>
<property name="url" value="http://www.baidu.com"></property>
<!-- 给Set的类型属性注入值 -->
<property name="numbers">
<list>
<value>23</value>
<value>34</value>
</list>
</property>
<property name="photo" value="d:\\bg.jpg"></property>
</bean>
</beans>