重写org.springframework.beans.BeanUtils的copyProperties方法,能在实体映射的时候把纯数字格式的日期转格式
就是在拷贝的时候加个正则的校验,如果是纯数字的日期 就转成yyyy-MM-dd HH:mm:ss的格式
原本想直接用注解在实体转格式,但是那样实体会变成日期格式,所以放弃了,直接重写拷贝的方法比较简单
public class BeanUtilsEx extends BeanUtils {
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来
* @param source
* @param target
* @throws BeansException
*/
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}
/**
* 重写方法,如果传入参数是纯数字的日期,转换后赋值
* @param source
* @param target
* @throws BeansException
*/
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
//年月日时分秒
String regexp1 = "^([0-9]{4})(0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)([0-2][0-9])([0-6][0-9])([0-6][0-9])$";
//年月日
String regexp2 = "^([0-9]{4})(0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)$";
//年月
String regexp3 = "^([0-9]{4})(0?[1-9]|1[0-2])$";
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
//将纯数字时间格式转换为标准格式
if (value != null) {
String dateStr = (String) value;
if (dateStr.matches(regexp1)) {
Date date = DateUtils.getDate(dateStr, "yyyyMMddHHmmss");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM-dd HH:mm:ss");
value = dateFormat;
}
if (dateStr.matches(regexp2)) {
Date date = DateUtils.getDate(dateStr, "yyyyMMdd");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM-dd");
value = dateFormat;
}
if (dateStr.matches(regexp3)) {
Date date = DateUtils.getDate(dateStr, "yyyyMM");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM");
value = dateFormat;
}
}
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
}
重写org.springframework.beans.BeanUtils的copyProperties方法,能在实体映射的时候把纯数字格式的日期转格式的更多相关文章
- org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别
知识点 org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作 ...
- org.springframework.beans.BeanUtils
org.springframework.beans.BeanUtils的一个demo.可以很优雅的实现将父类字段的值copy到子类中 下面例子的输出结果(子类使用父类的toString方法,有点意思吧 ...
- BeanUtils的copyproPerties方法的用法
转自:Hassan Blog的博客 一.简介: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包 ...
- org.springframework.beans.BeanUtils的用法
s,t为对象BeanUtils.copyProperties(s, t); 将给定源bean的属性值复制到目标bean中 public static void copyProperties(Obje ...
- BeanUtils工具类copyProperties方法缺点及解决
使用类为spring-beans:4.3.13release包中的 org.springframework.beans.BeanUtils BeanUtils.copyProperties(Objec ...
- 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...
- 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' define
本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...
- springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]
在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...
- 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...
- org.springframework.beans.factory.BeanCreationException: 求教育!
2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...
随机推荐
- ISTQB软件测试初级认证模拟题
参考地址 http://www.docin.com/p-297467364.html 第一章:软件测试基础(18%) 1.学习目标 1.1 为什么需要软件测试? (K2) ① 通过具体的例子,来描述软 ...
- 关于HTML5语义化
根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选项. HTML5 规范声明:应该使用 <h1> - <h6> 来表示标题,使用 ...
- rust crm 镜像源管理
一.下载crm cargo install crm https://github.com/wtklbm/crm 二.命令 # 在终端执行 # # NOTE: # - [args] 表示 args 是一 ...
- Word05 邀请函office真题
1.课程的讲解之前,先来对题目进行分析,首先需要制作一份请柬,请柬中需要包含标题.收件人名称.联谊会时间.联谊会地点和邀请人. 2.打开一个"新的Wrod"文档,在页面中输入请柬的 ...
- 新建VUE项目操作步骤(win7)
在win7下,新建一个VUE项目操作步骤说明 1 检查vue的相关软件环境 vue init webpack 项目名称 创建 vue 项目成功 进入到项目目录 然后 npm run dev 启 ...
- VS2010运行opencv的程序,出现“应用程序无法正常启动0xc000007b”的解决方法
问题描述 当我们在用vs2010对工程进行编译结束后,并且生成了可执行文件时,但是运用时却出现了"应用程序无法正常启动0xc000007b" 解决方法 这个通常是有一些动态链接库没 ...
- 5分钟上手使用vuex,vuex状态管理,vuex遇到的坑
很多刚学习vue的人对于全局变量管理工具vuex都觉得很神秘,今天就用很通俗的大白话协助大家理解一下vuex,作者的vue项目使用vue脚手架搭建的,用脚手架搭建的项目会在src文件夹下有一个stor ...
- python调用lua脚本
目录 lua代码 python代码 lua代码 入口函数是必须要填的 function test1(params) return 'test1:'..params end function test2 ...
- Test Fixture框架结构
Test Fixture框架 1.结构: setup() testcase() teardown() 2.新建unittest文件,命名unittestdemo.py 1 import unittes ...
- 攻防世界Web篇——unserialize3
知识点: 序列化与反序列化维基百科: 序列化(serialization)在计算机科学的数据处理中,是指将数据结构或对象状态转换成可取用格式(例如存成文件,存于缓冲,或经由网络中发送),以留待后 ...