Hobby.java
package com.wh.bean; public class Hobby { private Integer id; private String name; public Hobby() { // TODO Auto-generated constructor stub } public Hobby(Integer id, String name) { super(); this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Hobby [id=" + id + ", name=" + name + "]"; } }
Student.java
package com.wh.bean; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; import java.util.Set; public class Student { private Integer id; private String name; private String[] addressArray; private ArrayList<Hobby> hobbylist; private Set<Hobby> hobbysSet; private Map<String, String> map; public Student() { System.out.println("Student被创建了!!!"); } public Student(Integer id, String name, String[] addressArray, ArrayList<Hobby> hobbylist, Set<Hobby> hobbysSet, Map<String, String> map) { super(); this.id = id; this.name = name; this.addressArray = addressArray; this.hobbylist = hobbylist; this.hobbysSet = hobbysSet; this.map = map; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getAddressArray() { return addressArray; } public void setAddressArray(String[] addressArray) { this.addressArray = addressArray; } public ArrayList<Hobby> getHobbylist() { return hobbylist; } public void setHobbylist(ArrayList<Hobby> hobbylist) { this.hobbylist = hobbylist; } public Set<Hobby> getHobbysSet() { return hobbysSet; } public void setHobbysSet(Set<Hobby> hobbysSet) { this.hobbysSet = hobbysSet; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", addressArray=" + Arrays.toString(addressArray) + ", hobbylist=" + hobbylist + ", hobbysSet=" + hobbysSet + ", map=" + map + "]"; } }
applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean name="hobby01" class="com.wh.bean.Hobby">
<property name="id" value="1"/>
<property name="name" value="跑步"/>
</bean>
<bean name="hobby02" class="com.wh.bean.Hobby">
<property name="id" value="2"/>
<property name="name" value="游泳"/>
</bean>
<bean id="student" class="com.wh.bean.Student">
<property name="id" value="001"/>
<property name="name" value="zhangsan"/>
<!-- 数组 Property下的子标签既可以是list,也可以是array -->
<property name="addressArray">
<list>
<value>广贤路</value>
<value>三丰大夏</value>
</list>
</property>
<!-- list集合 -->
<property name="hobbylist">
<list>
<ref bean="hobby01"/>
<ref bean="hobby01"/>
</list>
</property>
<!-- set集合 -->
<property name="hobbysSet">
<set>
<ref bean="hobby02"/>
<ref bean="hobby02"/>
</set>
</property>
<!-- map集合 -->
<property name="map">
<map>
<!-- <entry key="" key-ref="" value="" value-ref=""></entry> -->
<entry key="mapKey01" value="mapValue01"/>
<entry key="mapKey02" value="mapValue02"/>
<entry key="mapKey03" value="mapValue03"/>
</map>
</property>
</bean>
</beans>
TestMVC.java
package com.wh.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wh.bean.Student; public class TestMVC { @Test public void testStudent(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu =(Student)ac.getBean("student"); System.out.println(stu); } }