package org.niit.springioc.example1; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Set; /**女孩拥有各种类型的猫对象*/ public class Girl implements Serializable { private String name; private Cat cat; private Cat[] cats; private List<Cat> catList; private Set<Cat> catSet; private Map<String,Cat> catMap; public Girl() { super(); } public Girl(String name, Cat cat, Cat[] cats, List<Cat> catList, Set<Cat> catSet, Map<String, Cat> catMap) { super(); this.name = name; this.cat = cat; this.cats = cats; this.catList = catList; this.catSet = catSet; this.catMap = catMap; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Cat[] getCats() { return cats; } public void setCats(Cat[] cats) { this.cats = cats; } public List<Cat> getCatList() { return catList; } public void setCatList(List<Cat> catList) { this.catList = catList; } public Set<Cat> getCatSet() { return catSet; } public void setCatSet(Set<Cat> catSet) { this.catSet = catSet; } public Map<String, Cat> getCatMap() { return catMap; } public void setCatMap(Map<String, Cat> catMap) { this.catMap = catMap; } } package org.niit.springioc.example1; import java.io.Serializable; public class Cat implements Serializable { private String id; private String color; public Cat(String id, String color) { super(); this.id = id; this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Cat() { super(); } public Cat(String id) { super(); this.id = id; } public String getId() { return id; } public void setId(String id) { this.id = id; } public void action(){ System.out.println("懒猫"+this.id+"在睡觉..."); } } 配置文件一: <?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 本实例演示: 1.属性注入 2.构造注入 3.bean引用 4.对象单个属性的注入 5.对数组属性的注入 --> <bean class="org.niit.springioc.example1.Girl" name="girl"> <property name="name" value="玛丽"></property> <property name="cat" ref="cat1"></property> <property name="cats"> <list> <ref bean="cat1"/> <ref bean="cat2"/> <bean class="org.niit.springioc.example1.Cat"> <property name="id" value="smalltigger3"></property> <property name="color" value="white"></property> </bean> </list> </property> </bean> <bean class="org.niit.springioc.example1.Cat" name="cat1"> <property name="id" value="smalltiger1"></property> <property name="color" value="white"></property> </bean> <bean class="org.niit.springioc.example1.Cat" name="cat2"> <constructor-arg index="0" value="smalltigger2"></constructor-arg> <constructor-arg index="1" value="black"></constructor-arg> </bean> </beans> 配置文件二: <?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 本实例演示: 集合的注入 --> <bean class="org.niit.springioc.example2.Girl" name="girl" scope="singleton" init-method="initilize" destroy-method="destroy"> <property name="name" value="玛丽"></property> <property name="catList"> <list> <ref bean="cat1"/> <ref bean="cat2"/> </list> </property> <property name="catSet"> <set> <ref bean="cat1"/> <ref bean="cat2"/> </set> </property> <property name="catMap"> <map> <entry key="cat1" value-ref="cat1"></entry> <entry key="cat2" value-ref="cat2"></entry> </map> </property> </bean> <bean class="org.niit.springioc.example2.Cat" name="cat1"> <property name="id" value="smalltiger1"></property> <property name="color" value="white"></property> </bean> <bean class="org.niit.springioc.example2.Cat" name="cat2"> <constructor-arg index="0" value="smalltigger2"></constructor-arg> <constructor-arg index="1" value="black"></constructor-arg> </bean> </beans> 测试代码一: package org.niit.springioc.example1; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class Client1 { private static BeanFactory bf = null; private static ApplicationContext appCxt = null; private static ApplicationContext getAppCxt() { if (appCxt==null){ /*实例化ApplicationContext接口对象的多种方式*/ //appCxt = new FileSystemXmlApplicationContext("c://applicationContext.xml"); appCxt=new ClassPathXmlApplicationContext("org/niit/springioc/example1/applicationContext.xml");; } return appCxt; } private static BeanFactory getBeanFactory() { if (bf==null){ Resource res = new ClassPathResource("org/niit/springioc/example1/applicationContext.xml"); bf = new XmlBeanFactory(res); } return bf; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Client1 c =new Client1(); BeanFactory bf = c.getBeanFactory(); Girl girl = (Girl)bf.getBean("girl"); System.out.println(girl.getName()+"有"+girl.getCats().length+"只猫"); for (Cat cat : girl.getCats()) { cat.action(); } } } 测试代码二: package org.niit.springioc.example2; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class Client1 { private static BeanFactory bf = null; private static ApplicationContext appCxt = null; private static ApplicationContext getAppCxt() { if (appCxt==null){ /*实例化ApplicationContext接口对象的多种方式*/ //appCxt = new FileSystemXmlApplicationContext("c://applicationContext.xml"); appCxt=new ClassPathXmlApplicationContext("org/niit/springioc/example2/applicationContext.xml");; } return appCxt; } private static BeanFactory getBeanFactory() { if (bf==null){ Resource res = new ClassPathResource("org/niit/springioc/example2/applicationContext.xml"); bf = new XmlBeanFactory(res); } return bf; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Client1 c =new Client1(); appCxt = c.getAppCxt(); Girl girl = (Girl)appCxt.getBean("girl"); //测试Set // System.out.println(girl.getName()+"有"+girl.getCatSet().size()+"只猫"); // for (Cat cat : girl.getCatSet()) { // cat.action(); // } //测试Map System.out.println(girl.getName()+"有"+girl.getCatMap().size()+"只猫"); for (String key : girl.getCatMap().keySet()) { girl.getCatMap().get(key).action(); } //((AbstractApplicationContext)appCxt).close();//一般只有Servlet容器或者EJB容器才会执行该方法 } }