码迷,mamicode.com
首页 > 编程语言 > 详细

spring配置自定义的PropertyEditor

时间:2015-04-13 12:56:39      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

       

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



PropertyEditor PropertyEditorSupport



技术分享


在上层是一个Property的简单接口,仅仅定义了setValue setAsText等几个非常简单的接口。后面通过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;
	}
	
	
	@Override
	public 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.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">

	<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());
	}
}

spring配置自定义的PropertyEditor

标签:

原文地址:http://blog.csdn.net/cfl20121314/article/details/45023357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!