这是一个Demo
1、Phone.java
package com.cn.pojo;public class Phone { private String name; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Phone(String name, double price) { super(); this.name = name; this.price = price; } public Phone() { super(); // TODO Auto-generated constructor stub }}
2、Student.java
package com.cn.pojo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Student { private int stuId; private String name; private Phone phone; private Listfavorties = new ArrayList ();; private Map jobTime = new HashMap (); public List getFavorties() { return favorties; } public void setFavorties(List favorties) { this.favorties = favorties; } public Map getJobTime() { return jobTime; } public void setJobTime(Map jobTime) { this.jobTime = jobTime; } public Phone getPhone() { return phone; } public void setPhone(Phone phone) { this.phone = phone; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student() { super(); System.out.println("come in..."); } public Student(int stuId, String name) { super(); this.stuId = stuId; this.name = name; } public void init(){ System.out.println("初始化。。。"); } public void destroy(){ System.out.println("销毁。。。"); }}
3、Test.java
package com.cn.test;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cn.pojo.Student;public class Test { @org.junit.Test public void test1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); System.out.println(stu.getName()); } @org.junit.Test public void test2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); System.out.println(stu.getPhone().getName()); } @org.junit.Test public void test3(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); Listfavs = stu.getFavorties(); for(String str : favs){ System.out.println(str); } } @org.junit.Test public void test4(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); Map maps = stu.getJobTime(); Set keys = maps.keySet(); Iterator iters = keys.iterator(); while(iters.hasNext()){ String key = iters.next(); System.out.println("key:"+key+",value:"+maps.get(key)); } } }
4、applicationContext.xml
吃饭 睡觉 看电影