package cn.lichen.bean;
import java.util.Date;
public class User {
private String name;
private int age;
private Date birthday;
private String[] anyPro;
private List<String> listPro;
private Set<String> setPro;
private Map<String, String> mapPro;
private Properties properties;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Date getBirthday() {
return birthday;
}
public String[] getAnyPro() {
return anyPro;
}
public List<String> getListPro() {
return listPro;
}
public Set<String> getSetPro() {
return setPro;
}
public Map<String, String> getMapPro() {
return mapPro;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setAnyPro(String[] anyPro) {
this.anyPro = anyPro;
}
public void setListPro(List<String> listPro) {
this.listPro = listPro;
}
public void setSetPro(Set<String> setPro) {
this.setPro = setPro;
}
public void setMapPro(Map<String, String> mapPro) {
this.mapPro = mapPro;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
util類:
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String format;
@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 String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
<?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.xsd"> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <!-- 內部Bean --> <bean class="cn.lichen.util.UtilDatePropertyEditor"> <property name="format"> <value>yyyy-MM-dd</value> </property> </bean> </entry> </map> </property> </bean> <bean id="user" class="cn.lichen.bean.User"> <!-- 設值注入:要求類提供無參的構造方法,必需提供屬性的seter()方法 --> <property name="name" value="LiChen"></property> <property name="age" value="22"></property> <property name="birthday" value="1992-2-6"></property> <property name="anyPro"> <array> <value>any1</value> <value>any2</value> <value>any3</value> </array> </property> <property name="listPro"> <list> <value>list1</value> <value>list2</value> <value>list3</value> </list> </property> <property name="setPro"> <set> <value>set1</value> <value>set2</value> <value>set3</value> </set> </property> <property name="mapPro"> <map> <entry key="1" value="map1"></entry> <entry key="2" value="map2"></entry> <entry key="3" value="map3"></entry> </map> </property> <property name="properties"> <props> <prop key="1">prop1</prop> <prop key="2">prop2</prop> <prop key="3">prop3</prop> </props> </property> </bean> </beans>
Test類:
package cn.lichen; import java.util.Iterator; public class TestUser { @Test public void testToString() { BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml"); User user = (User) factory.getBean("user"); System.out.println("My name is " + user.getName() + "\n" + user.getAge() + "\n" + user.getBirthday().toString()); // 數組 System.out.println(user.getAnyPro()[0]); // List集合 List<String> list = user.getListPro(); Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } // Set集合 Set<String> set = user.getSetPro(); for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) { String value = iterator.next(); System.out.println(value); } // map集合 Map<String, String> map = user.getMapPro(); Set<Entry<String, String>> entry = map.entrySet(); for (Iterator<Entry<String, String>> iterator = entry.iterator(); iterator .hasNext();) { Entry<String, String> mapEntry = iterator.next(); System.out.println(mapEntry.getKey() + "\t" + mapEntry.getValue()); } //properties集合,以前 都沒聽說 Properties properties = user.getProperties(); Set<String> setProperties = properties.stringPropertyNames(); for (Iterator<String> iterator = setProperties.iterator(); iterator .hasNext();) { String key = iterator.next(); String value = properties.getProperty(key); System.out.println("properties:" + "\t" + key + "\t" + value); } } }