import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 自定义属性编辑器
* @author yqs
*
*/
public class UtilDatePropertyEdit extends PropertyEditorSupport {
private String format = "yyyy/MM/dd";
public void setAsText(String text) throws IllegalArgumentException {
System.out.println(text + "----");
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.yqs.spring.UtilDatePropertyEdit">
<property name="format" value="yyyy-MM-dd" />
</bean>
</entry>
</map>
</property>
</bean>
<value>2008-11-25</value>
</property>
private BeanFactory factory;
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
}
public void testInjection() {
Bean1 bean1 = (Bean1)factory.getBean("bean1");
System.out.println(bean1.getStrValue());
System.out.println(bean1.getIntValue());
//System.out.println(bean1.getListValue());
List<String> list = bean1.getListValue();
for(String element : list) { //Enhanced for loop need Generic
System.out.println(element);
}
Set<String> set = bean1.getSetValue();
for(String str : set) {
System.out.println(str);
}
Map map = bean1.getMapValue();
Collection<String> coll = map.values();
for(String str : coll) {
System.out.println(str);
}
String[] strs = bean1.getArrayValue();
for(String str : strs) {
System.out.println(str);
}
Date d = bean1.getDateValue();
System.out.println(d);
Date d2 = bean1.getDateValue2();
System.out.println(d2);
}
}