首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring2.0(3) 自定义属性编辑器

2012-08-30 
spring2.0(三) 自定义属性编辑器?在Spring中,对象的普通属性(如int,String,List,Array,Set,Map等)可以通过

spring2.0(三) 自定义属性编辑器

?在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类中,提供了存放用户自定义编辑器的缓存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" 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" 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;@Overrideprotected 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即可。

热点排行