package com.zq.utils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
*
* Bean工具类
*
* Created by MyEclipse. Author: ChenBin E-mail: chenbin_2008@126.com Date:
* 2017年7月21日 Time: 下午3:59:08
*/
public class BeanUtils {

/**
* Description : 利用反射实现对象之间相同属性复制
*
* @author : ChenBin(E-Mail:chenbin_2008@126.com)
* @date : 2017年7月21日 下午4:47:07
* @param source
* 要复制的
* @param to
* 复制给
*/
public static void copyProperties(Object source, Object to) throws Exception {
copyPropertiesExclude(source, to, null);
}

/**
* Description : 复制对象属性
*
* @author : ChenBin(E-Mail:chenbin_2008@126.com)
* @date : 2017年7月21日 下午4:48:09
* @param from
* @param to
* @param excludsArray
* 排除属性列表
*/
public static void copyPropertiesExclude(Object from, Object to, String[] excludsArray) throws Exception {
List<String> excludesList = null;
if (excludsArray != null && excludsArray.length > 0) {
excludesList = Arrays.asList(excludsArray); // 构造列表对象
}
Method[] fromMethods = from.getClass().getMethods();
Method[] toMethods = to.getClass().getMethods();
Method fromMethod = null, toMethod = null;
String fromMethodName = null, toMethodName = null;
for (int i = 0; i < fromMethods.length; i++) {
fromMethod = fromMethods[i];
fromMethodName = fromMethod.getName();
if (!fromMethodName.contains("get"))
continue;
// 排除列表检测
if (excludesList != null && excludesList.contains(fromMethodName.substring(3).toLowerCase())) {
continue;
}
toMethodName = "set" + fromMethodName.substring(3);
toMethod = findMethodByName(toMethods, toMethodName);
if (toMethod == null)
continue;
Object value = fromMethod.invoke(from, new Object[0]);
if (value == null)
continue;
// 集合类判空处理
if (value instanceof Collection) {
Collection<?> newValue = (Collection<?>) value;
if (newValue.size() <= 0)
continue;
}
toMethod.invoke(to, new Object[] { value });
}
}

/**
* Description : 对象属性值复制,仅复制指定名称的属性值
*
* @author : ChenBin(E-Mail:chenbin_2008@126.com)
* @date : 2017年7月21日 下午4:48:36
*/
public static void copyPropertiesInclude(Object from, Object to, String[] includsArray) throws Exception {
List<String> includesList = null;
if (includsArray != null && includsArray.length > 0) {
includesList = Arrays.asList(includsArray);
} else {
return;
}
Method[] fromMethods = from.getClass().getDeclaredMethods();
Method[] toMethods = to.getClass().getDeclaredMethods();
Method fromMethod = null, toMethod = null;
String fromMethodName = null, toMethodName = null;
for (int i = 0; i < fromMethods.length; i++) {
fromMethod = fromMethods[i];
fromMethodName = fromMethod.getName();
if (!fromMethodName.contains("get"))
continue;
// 排除列表检测
String str = fromMethodName.substring(3);
if (!includesList.contains(str.substring(0, 1).toLowerCase() + str.substring(1))) {
continue;
}
toMethodName = "set" + fromMethodName.substring(3);
toMethod = findMethodByName(toMethods, toMethodName);
if (toMethod == null)
continue;
Object value = fromMethod.invoke(from, new Object[0]);
if (value == null)
continue;
// 集合类判空处理
if (value instanceof Collection) {
Collection<?> newValue = (Collection<?>) value;
if (newValue.size() <= 0)
continue;
}
toMethod.invoke(to, new Object[] { value });
}
}

/**
* Description : 从方法数组中获取指定名称的方法
*
* @author : ChenBin(E-Mail:chenbin_2008@126.com)
* @date : 2017年7月21日 下午4:48:49
*/
public static Method findMethodByName(Method[] methods, String name) {
for (int j = 0; j < methods.length; j++) {
if (methods[j].getName().equals(name)) {
return methods[j];
}
}
return null;
}

}

bean工具类的更多相关文章

  1. Spring获取bean工具类,可用于在线程里面获取bean

    Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...

  2. 获取spring上下文的bean 工具类

    有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...

  3. 【commons】Bean工具类——commons-beanutils之BeanUtils

    一.起步 引入依赖: <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> < ...

  4. Resultset转Bean工具类

    package org.pandas.webIdp.webOP.help; import java.lang.reflect.Field; import java.lang.reflect.Metho ...

  5. 获取Spring容器Bean对象工具类

    在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...

  6. java 常用Bean 转换工具类

    package com.hnf.framework.utils; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.data ...

  7. [性能] Bean拷贝工具类性能比较

    Bean拷贝工具类性能比较 引言 几年前做过一个项目,接入新的api接口.为了和api实现解耦,决定将api返回的实体类在本地也建一个.这样做有两个好处 可以在api变更字段的时候保持应用稳定性 可以 ...

  8. Spring的Bean,AOP以及工具类初探

    1.Bean(Ioc) BeanWrapper 根据JavaDoc中的说明,BeanWrapper提供了设置和获取属性值(单个的或者是批量的),获取属性描述信息.查询只读或者可写属性等功能.不仅如此, ...

  9. 可以随时拿取spring容器中Bean的工具类

    前言 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中.此时需 ...

随机推荐

  1. Android StickHeaderRecyclerView - 让recyclerview头部固定

    介绍在项目中有时会需要recyclerview滑动式时某个view滑出后会固定在头部显示,比较常用的比如手机联系人界面.地区选择界面等. StickHeaderRecyclerView就是实现这个功能 ...

  2. 【读书笔记】如何高效学习(Learn More ,Study Less)

    导读: 你会不会好奇为什么学习好的人会学的越来越好?那些课下不学习的人却比你考的好?一个人是怎么同时拿到好几个学位?为啥反复背的知识要领总是忘?为啥看个书总是不停走神?为啥总是苦逼似得看书直至厌烦? ...

  3. 触摸事件MotionEvent

    触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...

  4. Android自定义之ScrollView下拉刷新

    公司项目,需要用到ScrollView的下拉刷新,一开始使用的时候PullToRefresh三方库的下拉刷新,我比较纠结第三档库,很强大,但是,公司项目的需求,PullToRefresh就不能做到了, ...

  5. matlab练习程序(弧形投影)

    这个其实也算是圆柱体投影了,不过上一篇文章是从正面看,得到的是凸形的结果,而这个是从反面看,得到的是凹形的结果. 计算公式就不写了,大致介绍一下,计算公式中关于x坐标求法和上篇一样,y坐标则正好是上篇 ...

  6. java原生文件打包

    一.背景 前端时间,自己做的项目需要打包功能,不想再引外部的jar包 便用java.util.zip做了下该功能 二.适用场景 生成多个word.excel.xml等文件,并要求打包下载的情形 例:项 ...

  7. ARM 中可用性集使用的注意事项

    Azure 目前有两种部署模型:经典部署模型 (ASM) 和资源管理器 (ARM).如果您之前使用过 ASM 模式下的可用性集,那么很可能在使用 ARM 模式下的可用性集时,会遇到一些问题或者疑惑.这 ...

  8. String, StringBuffer and StringBuilder

    一 String 概述: String 被声明为 final,因此它不可被继承. 在 Java 8 中,String 内部使用 char 数组存储数据. public final class Stri ...

  9. Git软件的学习

    第一部分:我的git地址是https://github.com/ZHU19007/gitLearning 第二部分:我对git的认识 一.Git是一款免费.开源的分布式版本控制工具.而Github是用 ...

  10. 【洛谷5292】[HNOI2019] 校园旅行(思维DP)

    点此看题面 大致题意: 给你一张无向图,每个点权值为\(0\)或\(1\),多组询问两点之间是否存在一条回文路径. 暴力\(DP\) 首先,看到\(n\)如此之小(\(n\le5000\)),便容易想 ...