什么是属性编辑器,作用?
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法(注意要将处理完成的对象通过PropertyEditorSupport的setValue设置回去)
*向IoC容器中注册自定义的属性编辑器(两种方式:1 在配置文件中注册 2 在程序中注册)
接下来我们就看看一个关于日期的属性编辑器的部署过程:
首先定义一个测试类:
package com.property;
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;
}
}
然后继承PropertyEditorSupport,实现自定义的属性编辑器:
package com.property;
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(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;
}
}
接着我们首先定义配置文件:(采用配置方式)
<?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="user" class="com.property.User">
<property name="username" value="imaginecup"/>
<property name="birthday" value="1989-05-12"/>
</bean>
<!-- 自定义的属性编辑器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.property.UtilDatePropertyEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
package com.property;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class TestPropertyEditor {
public static void main(String[] args) {
// ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-*.xml");
// User user=(User)ac.getBean("user");
// System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
//它等价于
ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext-beans.xml"));
CustomEditorConfigurer configurer=(CustomEditorConfigurer)beanFactory.getBean("customEditorConfigurer");
configurer.postProcessBeanFactory((ConfigurableListableBeanFactory) beanFactory);
User user=(User)beanFactory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
输出结果:
name:imaginecup
birthday:Fri May 12 00:00:00 CDT 1989
接下来我们在程序中注册自定义的属性编辑器:
package com.property;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestPropertyEditorResgister {
public static void main(String[] args) {
ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext-beans.xml"));
beanFactory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(java.util.Date.class, new UtilDatePropertyEditor("yyyy-MM-dd"));
}
});
User user=(User)beanFactory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}