【小工具】根据定义的白名单字段进行Bean的拷贝
2017-12-30 17:45
411 查看
背景
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 + "]"; } } }
相关文章推荐
- List<Entity>排序:根据javabean中的一个字段对beanList进行排序
- [DataTable] datatable根据表中的字段进行排序
- (小工具)Java反射机制实现任意对象集合按指定任意字段进行分组通用程序实践
- 并编程对派出类的功能进行验证。。 轿车类等 派生出自行车类 停车等方法。然后从该类为基类 最大速度。重量等字段以及开车 要求具有速度 定义一个车辆类
- java护士排班 test 根据 1天24小时分成6班,一个护士一天上8个小时,一周上40小时,进行一周排班安排!拷贝,直接运行。
- Oracle根据多个字段进行查重
- JavaScript实用示例之根据其他字段对字段进行检查
- 容易遗忘的Comparable:一个结果集List,里面有很多的对象,要求根据对象的指定(age)字段进行排序
- 用arcgis根据字段进行分割数据
- Spring(16)——使用注解进行bean定义(二)
- Spring2.5 BeanPropertyRowMapper ResultSet和实体类的字段进行实现自动映射
- [SQL]根据刚刚插入数据的 ID 向另外一张表插入数据(插入的其他字段数据需要对字符串进行分析)
- EJB EntityBean 中,对自增字段的定义
- 根据value字段对map进行排序
- 加载指定包名下的所有类或根据类的annotation进行过滤的工具类
- mysql 中如何取得汉字字段的各汉字首字母, 并根据首字母进行查询
- oracle数据库中的某个字段根据中文拼音的首字母进行排序
- 泛型列表根据某个字段进行排序的用法
- php数组根据指定字段对数组进行排序函数
- 根据value字段对map进行排序