spring配置自定义的PropertyEditor
来源:互联网 发布:太平洋证券交易软件 编辑:程序博客网 时间:2024/05/18 01:58
在项目开发过程中,总会有这样那样由于格式转换带来的问题。如:money精确度的处理,这个应该在电商中非常常见。日期格式的处理,几乎是所有开发过程遇到的。本文通过介绍java.beans.PropertyEditor复习一下。
PropertyEditor、PropertyEditorSupport
在上层是一个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
- spring配置自定义的PropertyEditor
- Spring的自定义PropertyEditor
- spring 强大的PropertyEditor
- 探秘Spring的PropertyEditor
- Spring源代码分析(5)---propertyEditor(移花接木的自定义属性编辑器)
- 关于在Spring中注册自定义的PropertyEditor
- 关于在Spring中注册自定义的PropertyEditor
- Spring的FactoryBean与PropertyEditor
- Spring IOC 自定义属性编辑器PropertyEditor
- Spring PropertyEditor 相关类的模拟实现
- Spring Bean包装类案例之自定义PropertyEditor
- SpringMVC 自定义全局PropertyEditor
- SpringMVC 自定义全局PropertyEditor
- Spring bean配置中属性值由String到实际类型的动态转化过程及PropertyEditor类的应用
- spring propertyEditor 小总结
- Spring之PropertyEditor
- Spring PropertyEditor分析
- Spring源码解析-PropertyEditor
- IPv6 stack中的一些小小的surprise---(1)
- SIP相关的RFC文档
- apache 遇到 core 问题
- Mac OS 下 eclipse中文乱码解决方法(eclipse for mac 中文乱码)
- 字符集和字符编码(Charset & Encoding)
- spring配置自定义的PropertyEditor
- 索引实战
- ubuntu 14.04 gcc 版本的更新以及降级
- 爱快递查询
- MATLAB中使用DSPBuilder仿真时的设置
- VMware、VirtualBox安装工具包调整分辨率、设置鼠标无缝模式及共享剪切板等
- 矩阵快速幂求斐波那契通项(矩阵乘法优化线性递推式)
- c# 中各种类型的常用转换方式
- JNDI