PropertyEditor使用
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}
你可以把配置文件放在外面,当然配置文件的位置与名字就定死了,这样可以灵活一点