在Spring中,对象的普通属性(如int,String,List,Array,Set,Map等)可以通过配置文件注入到IoC Container,但是一些类型的属性(如Date等)必须通过某种转换器才可以注入到IoC容器并在程序中读取出正确的数据类型。Spring提供了 一种属性编辑器,可以将字符串(在配置文件中的都是字符串)转化为相应的对象,然后注入到其他对象中。
什么是自定义属性编辑器?
Spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器,自定义属性编辑器的作用是把Spring配置文件中的字符串转换成相应的对象进行注入。
自定义属性编辑器的实现思路:
1. 写一个工具类继承PropertyEditorSupport,重写setAsText(String text)方法。
2. org.springframework.beans.factory.config.CustomEditorConfigurer类中,提供了存放用户自定义编辑器的缓存private Map customEditorCache。所以,把这个类注入到IoC中,再使用private Map customEditorCache的setter方法把用户自定义的工具类注入到Map customEditors属性中(一般属性注入),就可以实现自定义属性编辑器。
下面以一个java.util.Date类型的自定义属性转换器来演示说明:
package com.wyx.spring; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; public class Bean { private String strValue; private int intValue; private List listValue; private Set setValue; private String[] arrayValue; private Map mapValue; private Date dateValue;//日期类型的转换不使用自定义属性编辑器,就会报错 getter and setter... }
applicationContext-beans.xml:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="myBean" class="com.wyx.spring.Bean"> <property name="strValue" value="hello" /> <property name="intValue" value="123"/> <property name="listValue"> <list> <value>list1</value> <value>list2</value> </list> </property> <property name="setValue"> <set> <value>set1</value> <value>set2</value> </set> </property> <property name="arrayValue"> <list> <value>array1</value> <value>array2</value> </list> </property> <property name="mapValue"> <map> <entry key="k1" value="v1"/> <entry key="k2" value="v2"/> </map> </property> <property name="dateValue"> <value>2008-1-1</value> </property> </bean> </beans>
applicationContext-editor.xml:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 定义属性编辑器 --> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <!-- setter注入,把一个自定义工具类注入给spring提供给我们的用户编辑器配置类CustomEditorConfigurer的customEditors(自定义编辑器的缓存)属性中--> <property name="customEditors"> <map> <entry key="java.util.Date"> <!-- 内部bean定义方式 可以省略id 只有包含它的customEditors可以使用 --> <bean class="com.wyx.spring.utils.UtilDatePropertyEditor"> <!-- setter方式注入,将format的格式注入给工具类的format属性,这样可以灵活的修改输入格式 --> <property name="format" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean> </beans>
用一个junit测试类来测试我们自定义的属性编辑器是否生效:
package com.wyx.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import junit.framework.TestCase; public class BeanTest extends TestCase { private BeanFactory bf = null; @Override protected void setUp() throws Exception { //初始化操作 //通配符,把全部applicationContext-开头的文件加载进来 bf = new ClassPathXmlApplicationContext("applicationContext-*.xml"); } public void testBean(){ Bean mybean = (Bean)bf.getBean("myBean"); System.out.println("mybean.strValue = " + mybean.getStrValue()); System.out.println("mybean.intValue = " + mybean.getIntValue()); System.out.println("mybean.setValue = " + mybean.getSetValue()); System.out.println("mybean.arrayValue = " + mybean.getArrayValue()); System.out.println("mybean.mapValue = " + mybean.getMapValue()); System.out.println("mybean.dataValue = " + mybean.getDateValue()); } }
打印:
mybean.strValue = hello
mybean.intValue = 123
mybean.setValue = [set1, set2, set3]
mybean.arrayValue = [Ljava.lang.String;@21b220
mybean.mapValue = {k1=v1, k2=v2}
mybean.dataValue = Tue Jan 01 00:00:00 CST 2008
要想修改接收字符串型日期类型的格式,只需要修改 applicationContext-editor.xml中format的value即可。