背景

Bean的拷贝一直有一些类可以使用,比如Apache的org.apache.commons.beanutils.BeanUtils或者Spring的org.springframework.beans.BeanUtils

根据定义的白名单字段进行Bean的拷贝

我需要一个只拷贝我指定的字段的Bean拷贝,而Spring的org.springframework.beans.BeanUtils提供如下几个方法:



其中第2、3个是可以指定属性的,第2个指定可以通过Class指定,基本满足我的需求;第3个指定无须理会的字段。

我不想定义另外的类,或者另外的父类去指定部分属性,我想通过自己配置的白名单只拷贝白名单指定的属性:

package com.nicchagil.exercise.springbootexercise.util;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import org.springframework.beans.BeanUtils; public class BeanCopyUtils { /**
* 根据白名单字段拷贝Bean
*/
public static <T> T copyProperties(Object source, Class<T> clazz, String[] whiteProperties) {
List<String> ignorePropertiesList = BeanCopyUtils.getIgnoreProperties(clazz, whiteProperties); String[] ignoreProperties = new String[ignorePropertiesList.size()];
ignorePropertiesList.toArray(ignoreProperties); T target;
try {
target = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("通过Class实例化对象发生异常", e);
} BeanUtils.copyProperties(source, target, ignoreProperties);
return target;
} /**
* 根据白名单字段字段获取无须理会的字段
* @param clazz
* @param whiteProperties
* @return
*/
public static List<String> getIgnoreProperties(Class<?> clazz, String[] whiteProperties) {
List<String> allProperties = BeanCopyUtils.getAllProperties(clazz); if (allProperties == null || allProperties.size() == 0) {
return null;
} allProperties.removeAll(Arrays.asList(whiteProperties));
return allProperties;
} /**
* 获取全部属性名称
*/
public static List<String> getAllProperties(Class<?> clazz) {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz); if (propertyDescriptors == null || propertyDescriptors.length == 0) {
return null;
} List<String> properties = new ArrayList<String>();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
properties.add(propertyDescriptor.getName());
} return properties;
} }

单元测试:

package com.nicchagil.exercise.springbootexercise;

import org.junit.Test;

import com.nicchagil.exercise.springbootexercise.util.BeanCopyUtils;

public class BeanCopyUtilsTest {

	@Test
public void test() {
User user = new User();
user.setId(123);
user.setName("Nick Huang"); User userCopy = BeanCopyUtils.copyProperties(user, User.class, new String[] {"name"});
System.out.println("user : " + user);
System.out.println("userCopy : " + userCopy);
} public static class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
} }

【小工具】根据定义的白名单字段进行Bean的拷贝的更多相关文章

  1. 数据库表转换成javaBean对象小工具

    package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...

  2. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  3. 开源小工具 酷狗、网易音乐缓存文件转mp3工具

    发布一个开源小工具,支持将酷狗和网易云音乐的缓存文件转码为MP3文件. 以前写过kgtemp文件转mp3工具,正好当前又有网易云音乐缓存文件需求,因此就在原来小工具的基础上做了一点修改,增加了对网易云 ...

  4. C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】

    C#7.2——编写安全高效的C#代码 2018-11-07 18:59 by 沉睡的木木夕, 123 阅读, 0 评论, 收藏, 编辑 原文地址:https://docs.microsoft.com/ ...

  5. Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)

                                                                                                        ...

  6. 这些小工具让你的Android 开发更高效

    在做Android 开发过程中,会遇到一些小的问题.尽管自己动手也能解决.可是有了一些小工具,解决这些问题就得心应手了,今天就为大家推荐一下Android 开发遇到的小工具,来让你的开发更高效. Vy ...

  7. Android 开发小工具之:Tools 属性 (转)

    Android 开发小工具之:Tools 属性 http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi 今天来介绍一些 Android 开发过程中比较有用但 ...

  8. html小工具——文章注释编辑器

    在网上阅读文章时,读者时常会想针对某段文字写一些自己的感想,可惜大部分阅读网站并不提供这样的功能,读者往往只能将文本复制到本地或在线的编辑器中编辑注释,之后如果想在其他地方回顾这些注释也必须先本地安装 ...

  9. [apue] 一个查看当前终端标志位设置的小工具

    话不多说,先看运行效果: >./term input flag 0x00000500 BRKINT not in ICRNL IGNBRK not in IGNCR not in IGNPAR ...

随机推荐

  1. 在cikuapi.com上抓取相关词

    最近用到文本相关性计算,要在开放域语料上操作,找了好久没找到好的方法,后来看到了清华的梁斌老师建的cikuapi,上面能查询一些相关词,自己写代码爬的时候出现中文解码问题,遂到Github上找了下相关 ...

  2. for each ...in / for ...in / for...of

    参考博客: https://www.cnblogs.com/ruoqiang/p/6217929.html https://www.cnblogs.com/dupd/p/5895474.html 1 ...

  3. PHP 入门学习教程及进阶(源于知乎网友的智慧)

    思过崖历程: 自学的动机.自学的技巧.自学的目标三个方面描述学习PHP的经历 一.自学的动机: 一定要有浓厚的兴趣,兴趣是最后的老师,可以在你迷茫的时候不断地支撑着你走下去. 自学不是为了工作,不是为 ...

  4. OpenCV3.3.0 + CLion + CMake 配置(Mac巨细无敌版)

    目录 开始 完成了 参考链接: 1,cmake编译 http://blog.csdn.net/baidu_36316735/article/details/53168438 2,CLion导入open ...

  5. BZOJ4081 : [Wf2014]Skiing

    首先将目标点按$y$坐标从小到大排序. 如果加速度为$0$,那么只要贪心走一遍即可. 否则考虑DP,设$f[i][j]$表示从$i$点以速度$j$出发最多能经过多少个点. 注意到将DP值相同的合并可以 ...

  6. yii2 配合bootstrap添加一个气泡

    添加一个气泡 1.bootstrap 官网:http://getbootstrap.com/ 2.bootstrap 中文官网:http://v3.bootcss.com/ 添加气泡主要需要用到 bo ...

  7. java内存和linux关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  8. mysql:Cannot proceed because system tables used by Event Scheduler were found damaged at server start

    mysql 5.7.18 sqlyog访问数据库,查看表数据时,出现 Cannot proceed because system tables used by Event Scheduler were ...

  9. Sublime_分屏显示

  10. vue中 如何使用less

    首先肯定是vue-cli全部就位: 1,安装依赖: npm install less less-loader --save 2,修改build-webpack.base.config.js文件,配置l ...