第一步:新建工程 SecondSpring
文件目录结构如下:
第二步: 导入相关的spring jar包
略...
第三步: 新建类
GatherTest.java
package com.xuzhiwen.spring5; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class GatherTest { public List<Object> list; public List<Object> objlist; public Map<String,Object> map; public Set<Object> set; public Properties properties; public void setList(List<Object> list) { this.list = list; } public void setObjlist(List<Object> objlist) { this.objlist = objlist; } public void setMap(Map<String, Object> map) { this.map = map; } public void setSet(Set<Object> set) { this.set = set; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "GatherTest [list=" + list +", objlist=" + objlist + ", map=" + map + ", set=" + set + ", properties=" + properties + "]"; } }
Person1.java
package com.xuzhiwen.spring5; public class Person1 { public String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Person1 [name=" + name + "]"; } }
Person2.java
package com.xuzhiwen.spring5; public class Person2 { public String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Person2 [name=" + name + "]"; } }
第四步: 新建配置文件
common.xml
<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-2.5.xsd"> <import resource="xmlfolder/app1.xml" /> <import resource="xmlfolder/innerbean.xml" /> <import resource="xmlfolder/singleton.xml" /> <import resource="xmlfolder/annotation.xml" /> <import resource="xmlfolder/gather.xml" /> </beans>
annotation.xml
<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.5.xsd"> <bean id="gather" class="com.xuzhiwen.spring5.GatherTest"> <!-- 给list赋值 ======================================================--> <property name="list"> <list> <value>张三</value> <value>李四</value> <value>王五</value> </list> </property> <property name="objlist"> <list> <ref bean="person1" /> <ref bean="person2" /> </list> </property> <!-- 给map赋值 ======================================================--> <property name="map"> <map> <entry key="key1" value="value1" /> <entry key="key2" value="value2" /> <entry key="key3" value="value3" /> <entry key="key4" value-ref="person1" /> <entry key="key5" value-ref="person2" /> </map> </property> <!-- 给set赋值 ======================================================--> <property name="set"> <set> <ref local="person1" /> <ref local="person2" /> </set> </property> <!-- 给properties赋值============================================= --> <property name="properties"> <props> <prop key="aaaa">aaaa</prop> <prop key="bbbb">bbbb</prop> </props> </property> </bean> <!-- ====================================================== --> <!-- person1 --> <bean id="person1" class="com.xuzhiwen.spring5.Person1"> <property name="name" value="xuzhiwen" /> </bean> <!-- person2 --> <bean id="person2" class="com.xuzhiwen.spring5.Person2"> <property name="name" value="fanjialing" /> </bean> </beans>
第五步:新建测试类
Test.java
package com.xuzhiwen.spring5; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("common.xml"); GatherTest test = (GatherTest) app.getBean("gather"); System.out.println(test); } }
第六步:运行结果如下
GatherTest [list=[张三, 李四, 王五],
objlist=[Person1 [name=xuzhiwen], Person2 [name=fanjialing]],
map={key1=value1, key2=value2, key3=value3, key4=Person1 [name=xuzhiwen], key5=Person2 [name=fanjialing]},
set=[Person1 [name=xuzhiwen], Person2 [name=fanjialing]],
properties={aaaa=aaaa, bbbb=bbbb}]