作者在 2010-06-07 11:29:05 发布以下内容
bean.xml代码
<?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="student" class="com.yds.test1.day1.Student">
<property name="name">
<value>你好</value>
</property>
<property name="usermsg">
<list>
<value>你好啊</value>
<value>你一定很好</value>
<value>list结束</value>
</list>
</property>
<property name="userSet">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
<value>第一个结束</value>
</set>
</property>
<property name="userMap">
<map>
<entry>
<key><value>1</value></key>
<value>你好啊</value>
</entry>
<entry>
<key><value>2</value></key>
<value>Map结束</value>
</entry>
</map>
</property>
<property name="userDate">
<value>1989-11-02 13:32:12</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry>
<key>
<value>java.util.Date</value>
</key>
<bean class="com.yds.test1.day1.EditDate"></bean>
</entry>
</map>
</property>
</bean>
</beans>
实体类<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="student" class="com.yds.test1.day1.Student">
<property name="name">
<value>你好</value>
</property>
<property name="usermsg">
<list>
<value>你好啊</value>
<value>你一定很好</value>
<value>list结束</value>
</list>
</property>
<property name="userSet">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
<value>第一个结束</value>
</set>
</property>
<property name="userMap">
<map>
<entry>
<key><value>1</value></key>
<value>你好啊</value>
</entry>
<entry>
<key><value>2</value></key>
<value>Map结束</value>
</entry>
</map>
</property>
<property name="userDate">
<value>1989-11-02 13:32:12</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry>
<key>
<value>java.util.Date</value>
</key>
<bean class="com.yds.test1.day1.EditDate"></bean>
</entry>
</map>
</property>
</bean>
</beans>
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student implements Serializable {
private String name;
private String pass;
private List usermsg;
private Set userSet;
private Map userMap;
private Date userDate;
public Date getUserDate() {
return userDate;
}
public void setUserDate(Date userDate) {
this.userDate = userDate;
}
public Map getUserMap() {
return userMap;
}
public void setUserMap(Map userMap) {
this.userMap = userMap;
}
public Set getUserSet() {
return userSet;
}
public void setUserSet(Set userSet) {
this.userSet = userSet;
}
public List getUsermsg() {
return usermsg;
}
public void setUsermsg(List usermsg) {
this.usermsg = usermsg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public void toString1(){
System.out.println("成功");
}
}
测试类(采用junit)也可以自己写一个test类import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student implements Serializable {
private String name;
private String pass;
private List usermsg;
private Set userSet;
private Map userMap;
private Date userDate;
public Date getUserDate() {
return userDate;
}
public void setUserDate(Date userDate) {
this.userDate = userDate;
}
public Map getUserMap() {
return userMap;
}
public void setUserMap(Map userMap) {
this.userMap = userMap;
}
public Set getUserSet() {
return userSet;
}
public void setUserSet(Set userSet) {
this.userSet = userSet;
}
public List getUsermsg() {
return usermsg;
}
public void setUsermsg(List usermsg) {
this.usermsg = usermsg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public void toString1(){
System.out.println("成功");
}
}
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanTest {
@Test public void insett(){
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
Student st=(Student)ctx.getBean("student");
System.out.println(st.getName());
List m=st.getUsermsg();
for(int i=0;i<m.size();i++){
System.out.println("List中的值"+m.get(i));
}
Set s=st.getUserSet();
Iterator ir=s.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
}
Map mp=st.getUserMap();
//Map的 遍历
/*Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.print(entry.getKey());
System.out.println(entry.getValue());
}*/
for(Object o:mp.keySet()){
System.out.println(mp.get(o));
}
Date date=st.getUserDate();
DateFormat df=new SimpleDateFormat("yyyy年mm月dd日 hh时mm分ss秒");
String d=df.format(date);
System.out.println("用户时间"+d);
st.toString1();
}
}
日期转型类import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanTest {
@Test public void insett(){
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
Student st=(Student)ctx.getBean("student");
System.out.println(st.getName());
List m=st.getUsermsg();
for(int i=0;i<m.size();i++){
System.out.println("List中的值"+m.get(i));
}
Set s=st.getUserSet();
Iterator ir=s.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
}
Map mp=st.getUserMap();
//Map的 遍历
/*Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.print(entry.getKey());
System.out.println(entry.getValue());
}*/
for(Object o:mp.keySet()){
System.out.println(mp.get(o));
}
Date date=st.getUserDate();
DateFormat df=new SimpleDateFormat("yyyy年mm月dd日 hh时mm分ss秒");
String d=df.format(date);
System.out.println("用户时间"+d);
st.toString1();
}
}
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class EditDate extends PropertyEditorSupport {
public void setAsText(String str) throws IllegalArgumentException {
DateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
try {
Date d=df.parse(str);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class EditDate extends PropertyEditorSupport {
public void setAsText(String str) throws IllegalArgumentException {
DateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
try {
Date d=df.parse(str);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}