先建立一个Bean:
Code
package zhuru;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Bean {
private int id;
private String name;
private String[] stringargs;
private List listargs;
private Set setargs;
private Map mapargs;
private Date date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getStringargs() {
return stringargs;
}
public void setStringargs(String[] stringargs) {
this.stringargs = stringargs;
}
public List getListargs() {
return listargs;
}
public void setListargs(List listargs) {
this.listargs = listargs;
}
public Set getSetargs() {
return setargs;
}
public void setSetargs(Set setargs) {
this.setargs = setargs;
}
public Map getMapargs() {
return mapargs;
}
public void setMapargs(Map mapargs) {
this.mapargs = mapargs;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
Spring的配置文件如下:
Code
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean id="Bean" class="zhuru.Bean">
<property name="id" value="009" />
<property name="name" value="tom" />
<property name="stringargs">
<list>
<value>1000</value>
<value>1001</value>
</list>
</property>
<property name="listargs">
<list>
<value>10002</value>
<value>10003</value>
</list>
</property>
<property name="setargs">
<set>
<value>中国</value>
<value>美国</value>
<value>俄罗斯</value>
</set>
</property>
<property name="mapargs">
<map>
<entry key="m1">
<value>map1</value>
</entry>
<entry key="m2">
<value>map2</value>
</entry>
<entry key="m3">
<value>map3</value>
</entry>
</map>
</property>
<property name="date">
<value>2009/11/26</value>
</property>
</bean>
<!--注册属性编辑器-->
<bean id="custromEditroConfig"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="zhuru.DatePropertyEditor">
<property name="format" value="yyyy/MM/dd" />
</bean>
</entry>
</map>
</property>
</bean>
</beans>
ps:内部bean(inner bean)是指在一个bean的<property/>
或 <constructor-arg/>
元素中使用<bean/>
元素定义的bean。内部bean定义不需要有id或name属性,即使指定id 或 name属性值也将会被容器忽略。
属性编辑器如下(实现java.beans.PropertyEditorSupport类的setAsText方法):
Code
package zhuru;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport{
private String format="yyyy-MM-dd";
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf=new SimpleDateFormat(format);
try {
Date date= sdf.parse(text);
this.setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
建立单元测试类
Code
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zhuru.Bean;
public class TestZhuru {
ApplicationContext context=null;
@Before
public void init(){
context=new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void zuruTest(){
Bean bean=(Bean)context.getBean("Bean");
System.out.println("id:"+bean.getId());
System.out.println("name:"+bean.getName());
System.out.println("stringargs:"+bean.getStringargs());
System.out.println("lisrargs:"+bean.getListargs());
System.out.println("setargs:"+bean.getSetargs());
System.out.println("mapargs:"+bean.getMapargs());
System.out.println("date:"+bean.getDate());
}
}
测试结果如下:
id:9
name:tom
stringargs:[Ljava.lang.String;@2cb49d
lisrargs:[10002, 10003]
setargs:[中国, 美国, 俄罗斯]
mapargs:{m1=map1, m2=map2, m3=map3}
date:Thu Nov 26 00:00:00 CST 2009
在单元测试中添加新的方法:
@Test
public void isSameTest(){
Bean bean1=(Bean)context.getBean("Bean");
Bean bean2=(Bean)context.getBean("Bean");
if(bean1==bean2){
System.out.println("bean1==bean2");
}else{
System.out.println("bean1!=bean2");
}
}
测试结果为:bean1==bean2
这是因为Bean的作用域,默认为singleton,所以每次调用getBean()都返回同一个实例,处于线程安全考虑,可以把bean的scope设置prototype,这样就会返回不同的实例。
<bean id="Bean" class="zhuru.Bean" scope="prototype">
重新测试结果为:bean1!=bean2