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. cf1072B. Curiosity Has No Limits(枚举)

    题意 题目链接 给出两个序列\(a, b\),求出一个序列\(t\),满足 \[a_i = t_i | t_{i + 1}\] \[b_i = t_i \& t_{i + 1}\] 同时,\( ...

  2. sort属性

    学习文章---链接 总结笔记 ①sort是Array.prototype的属性, ②如果不写入参数,则按照转换为的字符串的每个字符的unicode位点进行排序, ③如果传入一个比较函数sort(fun ...

  3. flex布局帮助你快速实现布局

    flex布局可以帮我们快速布局一些区块,实现你想要的效果,不用再去float,position之类的.我们在布局网页的时候很多时候都是一些特殊布局,flex就能帮我快速去布局,不需要去定位. 任何一个 ...

  4. FCKEditor编辑器添加中文字体的方法

    默认情况下,FCKEditor在进行文本编辑时,无法使用中文字体.让其添加中文字体的方法: 1.打开 fckconfig.js 文件,找到第154行(大概),会发现: 程序代码: FCKConfig. ...

  5. 类数组arguments

    var isArray = function(){ return arguments; } isArray(1,2,3); // 返回[1,2,3] isArray.call(null,1,2,3); ...

  6. 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述

    1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...

  7. Div+Css布局教程(-)CSS必备知识

    目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...

  8. Linux下创建vue项目

    前提:已经安装了node.js.cnpm 1.全局安装vue脚手架vue-cli:#cnpm install -g vue-cli 注意:全局安装没有建立软链接前是无法使用刚刚安装的vue命令的,所以 ...

  9. Struts2的学习-通配符和session对象

    一. 取得session 3种方法1.context.getSession() -->>Map对象 2.HttpServletRequest request =(HttpServletRe ...

  10. Android 位置服务

    原文来自:http://developer.android.com/guide/topics/location/strategies.html 位置策略 注意: 本指南仅限android.locati ...