spring配置自定义的PropertyEditor

来源:互联网 发布:太平洋证券交易软件 编辑:程序博客网 时间:2024/05/18 01:58

       

        在项目开发过程中,总会有这样那样由于格式转换带来的问题。如:money精确度的处理,这个应该在电商中非常常见。日期格式的处理,几乎是所有开发过程遇到的。本文通过介绍java.beans.PropertyEditor复习一下。



PropertyEditorPropertyEditorSupport



计算机生成了可选文字:OProper钾Ed而rPropertYEd比o6uppert如图芡常简牟哟类和接口的关系。O刁teUtil


在上层是一个Property的简单接口,仅仅定义了setValuesetAsText等几个非常简单的接口。后面通过PropertyEditorSupport来做了一些实现。最常用的方法也就是setAsText setValue分别是用来转换字符串,以及注入转换后的值。



DataUtil


package cfl.spring.beans;import java.beans.PropertyEditorSupport;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class DateUtil extends PropertyEditorSupport {    private String pattern;public String getPattern() {return pattern;}public void setPattern(String pattern) {this.pattern = pattern;}@Overridepublic void setAsText(String text) throws IllegalArgumentException {try {    SimpleDateFormat df=new SimpleDateFormat(pattern ,Locale.CHINA);    Date date=df.parse(text);this.setValue(date);System.out.println(date);} catch (ParseException e) {e.printStackTrace();}}}


自定义格式处理的字符串的类。通过重写父类的方法来根据项目开发的需求,自定义格式处理字符串。

同时将需要配置为何种格式的日期类型,通过spring注入。利于手动灵活配置。


配置到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: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.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="customer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors"><map><entry key="java.util.Date"><bean id="dateUtil" class="cfl.spring.beans.DateUtil"><property name="pattern"><value>yyyy-MM-dd</value></property></bean></entry></map></property></bean></beans>


贴上测试类吧,方便阅读

    
package cfl.test.configure;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import cfl.spring.beans.Bean1;import junit.framework.TestCase;public class configureTest extends TestCase{private BeanFactory beanfactory;protected void setUp() throws Exception {//方式1:读取单个xml文件//beanfactory=new ClassPathXmlApplicationContext("applicationContext.xml");//方式2:读取多个xml文件String[] strings=new String[]{"applicationContext-basic.xml","applicationContext-edit.xml"};//beanfactory=new ClassPathXmlApplicationContext(strings);//方式3:读取指定规格的xml文件beanfactory =new ClassPathXmlApplicationContext(strings);}public void testConfigure(){Bean1 bean1=(Bean1)beanfactory.getBean("Bean1");System.out.println(bean1.getBean2().getAge());System.out.println(bean1.getBean3().getValueList());System.out.println(bean1.getBean3().getDataValue());}}

0 0