一、首先定义一个用于演示原型模式下Bean实例化的User类,User实例的ID是随机生成的整数:
package test.spring.xml;
import java.util.Random;
//用户持久化类
public class User {
Integer id; //自然ID号
String userName; //用户名
String userPwd; //用户密码
Random rnd =new Random();
//默认构造方法
public User(){
this.id =rnd.nextInt(1000); //产生一个1000以内的随机ID
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public Random getRnd() {
return rnd;
}
public void setRnd(Random rnd) {
this.rnd = rnd;
}
}
二、接下来定义一个包含各种需求演示的数据类型属性,自定义构造方法,初始化方法与销毁方法等的业务类XmlIocBean
package test.spring.xml;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**用于演示XML方式装备Bean实例的测试类*/
public class XmlIocBean {
//定义两个通过构造方法注入的属性
Date now;
SimpleDateFormat sf;
//定义集合类型的属性
int [] intnumber;
List list;
Set set;
Map map;
Properties props;
//默认构造方法
public XmlIocBean (){}
//自定义构造方法
public XmlIocBean(SimpleDateFormat sf,Date now){
this.now=now;
this.sf=sf;
}
//定义初始化方法
public void myInit(){
System.out.println("XmlIocBean类的初始化方法myInit()被调用了!");
}
//定义一个业务方法输出各属性值
public void execute(){
System.out.println("现在的时间是:"+sf.format(now));
System.out.println("intnumber数组元素内容为:");
for(int i:intnumber){
System.out.println("\t"+i);
}
System.out.println("\r\n list实例中的元素内容为:");
for(Object obj:list){
if(obj instanceof User) System.out.println("\t 用户ID="+((User)obj).getId());
else System.out.println("\t"+obj);
}
System.out.println("\r\n set实例中的元素内容为:");
for(Object obj:set){
System.out.println("\t"+obj+"\t");
}
System.out.println("\r\tmap实例中的元素内容为:");
for(Object key:map.keySet()){
if(map.get(key) instanceof User){
System.out.println("\t 用户ID="+((User)map.get(key)).getId());
}else{
System.out.println("\t"+key+"="+map.get(key));
}
}
System.out.println("\r\nprops实例中元素内容为:");
for(Object key:props.keySet()){
System.out.println("\t"+key+"="+props.get(key));
}
System.out.println();
}
//定义销毁方法
public void myDestory(){
System.out.println("XmlIocBean类的销毁方法myDestory被调用了!");
}
//省略各属性的set、get方法
}
三、下面在Spring的XMl配置文件中配置User与XmlIocBean类的装配信息:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 通过构造方法注入属性值装配SimpleDateFormat单实例 -->
<bean id="simpleDateFormat" class="java.text.SimpleDateFormat">
<!-- 在注入构造参数时,如果只有一个字符串类型的参数,则可省略index与type属性 -->
<constructor-arg value="yyyy年MM月dd日 hh时mm分ss秒"/>
</bean>
<!-- 使用原型模式装配User实例 -->
<bean id ="user" class="test.spring.xml.User" scope="prototype">
<!-- 使用set方式注入属性值 -->
<property name="userName" value="lilubin"/>
<property name="userPwd" value="123456"/>
</bean>
<!-- 使用单例模式装配XmlIocBean实例,同时制定初始化方法与销毁方法 -->
<bean id="xmlIocBean" class="test.spring.xml.XmlIocBean" init-method="myInit" destroy-method="myDestory">
<!-- 使用构造方法注入 -->
<constructor-arg index="0" type="java.text.SimpleDateFormat" ref="simpleDateFormat"/>
<constructor-arg index="1" type="java.util.Date">
<!-- 定义内部Bean实例 -->
<bean class="java.util.Date"/>
</constructor-arg>
<!-- 使用set方法注入数组类型属性 -->
<property name="intnumber" >
<list>
<value>2</value>
<value>16</value>
<value>43</value>
</list>
</property>
<!-- 使用set方式注入List类型数据 -->
<property name="list">
<list>
<value>元素1</value>
<!-- 由于上面的User实例使用原型模式装配,每次引用的将是全新的User实例 -->
<ref bean="user"/>
<ref bean="user"/>
</list>
</property>
<!-- 使用set方式注入Set类型属性 -->
<property name="set">
<set>
<value>set元素1</value>
<value>set元素2</value>
</set>
</property>
<!-- 使用set方式注入Map类型属性 -->
<property name="map">
<map>
<entry key="key1"><value>map元素1</value></entry>
<entry key="key2"><ref bean="user"/></entry>
</map>
</property>
<!-- 使用set方式注入Properties类型属性 -->
<property name="props">
<props>
<prop key="prop1">properties元素1</prop>
<prop key="prop2">properties元素2</prop>
</props>
</property>
</bean>
</beans>
四、最后编写XmlIocBean类的Junit测试用例testXmlIocBean:
package test.spring.junit;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.xml.XmlIocBean;
public class testXmlIcoBean {
static AbstractApplicationContext cxt;
static XmlIocBean xmlIocBean;
//初始化Application容器
@BeforeClass
public static void setUpBeforeClass()throws Exception{
//使用ClassPathXmlApplicationContext方式初始化ApplicationContext容器
cxt=new ClassPathXmlApplicationContext("xmlIocBean.xml");
xmlIocBean =(XmlIocBean) cxt.getBean("xmlIocBean");
}
@Test
public void testExecute(){
xmlIocBean.execute();
//手动关闭Spring容器
cxt.close();
}
}
六、测试用例运行的结果如图:
XmlIocBean类的初始化方法myInit()被调用了!
现在的时间是:2012年10月09日 10时48分24秒
intnumber数组元素内容为:
2
16
43
list实例中的元素内容为:
元素1
用户ID=218
用户ID=927
set实例中的元素内容为:
set元素1
set元素2
map实例中的元素内容为:
key1=map元素1
用户ID=858
props实例中元素内容为:
prop2=properties元素2
prop1=properties元素1
XmlIocBean类的销毁方法myDestory被调用了!