PropertyEditor使用
假如我們的有如下類 其中date是你的生日,而你在配置文件中,或者使用注解都是字符串,所以我們有必要把字符串轉換成Date類型的
package liusheng.entity; import java.util.Date; public class User { private String name; private Integer id; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } private Date date; public String toString() { return "User{" + "name='" + name + '\'' + ", id=" + id + ", date=" + date + '}'; } public User(String name, Integer id) { this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public User() { } }
注意Spring中的Date轉換器(CustomDateEditor),不能有參數,所以不能使用(Spring 5xx),我試過無法注入到
CustomEditorConfigurer的屬性中,應為該屬性是一個Map<Class,Class<? extends PropertyEditor>>
而spring中的Class類型只能通過字符串,所以不能有參數,所以轉換器只能在類中寫死
如:
package liusheng.propertyEidtor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @Component("myDateEditor") public class MyDateEditor extends PropertyEditorSupport { private List<SimpleDateFormat> list; { list=new ArrayList<SimpleDateFormat>(); list.add(new SimpleDateFormat("yyyy年MM月dd日")); } public String getAsText() { return getValue().toString(); } public List<SimpleDateFormat> getList() { return list; } public void setList(List<SimpleDateFormat> list) { this.list = list; } public void setAsText(String text) throws IllegalArgumentException { for (SimpleDateFormat format: list) { try { Date date = format.parse(text); setValue(date); return ; } catch (ParseException e) { } } throw new RuntimeException("無法轉換"); } }
配置文件如下:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <context:component-scan base-package="liusheng"/> <bean class="liusheng.entity.User" name="user"> <property name="date" value="1234年12月18日"/> <property name="id" value="10"/> <!--//--> <property name="name" value="張三"/> </bean> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors" > <map> <entry key="java.util.Date" value="liusheng.propertyEidtor.MyDateEditor" > </entry> </map> </property> </bean> <util:list id="dateList"> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy年MM月dd日"></constructor-arg> </bean> </util:list> </beans>
輸出:
User{name='張三', id=10, date=Mon Dec 18 00:00:00 CST 1234}
你可以把配置文件放在外面,當然配置文件的位置與名字就定死了,這樣可以靈活一點
