什么是属性编辑器,作用?
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java
自定义属性编辑器:UtilDatePropertyEditor.java 如下,必须继承java.beans.PropertyEditorSupport类,覆写setAsText()方法 。
spring配置文件:
测试类:
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java
* 将属性编辑器注册到spring中,参见:applicationContext.xml ;
直接上代码。
实体类:
package com.spring.propertyEditor;
import java.util.Date;
public class User {
private String username;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
自定义属性编辑器:UtilDatePropertyEditor.java 如下,必须继承java.beans.PropertyEditorSupport类,覆写setAsText()方法 。
package com.spring.propertyEditor;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String format;
public UtilDatePropertyEditor() {
}
public UtilDatePropertyEditor(String format) {
this.format = format;
}
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(text);
this.setValue(date);
} catch (ParseException e) {
System.out.println(e.getMessage() + "日期的格式不对");
}
}
public void setFormat(String format) {
this.format = format;
}
}
spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" >
<bean id="user" class="com.spring.propertyEditor.User">
<property name="username">
<value>test</value>
</property>
<property name="birthday">
<value>2008-08-15</value>
</property>
</bean>
<!-- 于是定义属性编辑器 -->
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.spring.propertyEditor.UtilDatePropertyEditor">
<!--干脆把format也注入,灵活处理格式 -->
<property name="format">
<value>yyyy-MM-dd</value>
</property>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
测试类:
package com.spring.propertyEditor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPropertyEditor {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
User user = (User) beanFactory.getBean("user");
System.out.println("name:" + user.getUsername() + "\nbirthday:"
+ user.getBirthday());
}
}