bean工具类
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工具类的更多相关文章
- Spring获取bean工具类,可用于在线程里面获取bean
Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...
- 获取spring上下文的bean 工具类
有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...
- 【commons】Bean工具类——commons-beanutils之BeanUtils
一.起步 引入依赖: <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> < ...
- Resultset转Bean工具类
package org.pandas.webIdp.webOP.help; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- 获取Spring容器Bean对象工具类
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...
- java 常用Bean 转换工具类
package com.hnf.framework.utils; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.data ...
- [性能] Bean拷贝工具类性能比较
Bean拷贝工具类性能比较 引言 几年前做过一个项目,接入新的api接口.为了和api实现解耦,决定将api返回的实体类在本地也建一个.这样做有两个好处 可以在api变更字段的时候保持应用稳定性 可以 ...
- Spring的Bean,AOP以及工具类初探
1.Bean(Ioc) BeanWrapper 根据JavaDoc中的说明,BeanWrapper提供了设置和获取属性值(单个的或者是批量的),获取属性描述信息.查询只读或者可写属性等功能.不仅如此, ...
- 可以随时拿取spring容器中Bean的工具类
前言 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中.此时需 ...
随机推荐
- angular2-生命周期钩子函数
生命周期的顺序 当Angular使用构造函数新建一个组件或指令后,就会按下面的顺序在特定时刻调用这些生命周期钩子方法: 钩子 目的和时机 ngOnChanges() 当Angular(重新)设置数据绑 ...
- LARAVEL学习--安装
之前一直使用Codeignitor框架进行PHP的开发,Codeignitor是一个非常优秀的框架,上手简单,文档极其友好,流行程度甚高(这带来了很好的社区支持+云环境支持),很轻量,可扩展性佳,性能 ...
- JavaScript中文拼音排序函数
要对很多设备根据名称排序,找了找没有找到特别适合的,然后就自己写了一个根据中文拼音首字母排序的方法. github: https://github.com/haboll/sort.git
- SharePoint 2013 - Sideloading
默认情况下,App是不能直接部署到Production环境,只能通过App Catalog中安装. 只有在Developer类型站点中才默认激活了Developer feature. 所以如果想要使用 ...
- Python学习系列----第六章 数据结构
本章主要讲的是python中重要的四种数据结构,分别是列表.元组.字典和集合. 6.1 列表 list 是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目应该包括在方括 ...
- Win SDK 标准程序
//-Registering window class. //-Creating window. //-Creating message loop. //-Implement WndProc func ...
- 将CSV文件中的数据导入到SQL Server 数据库中
导入数据时,需要注意 CSV 文件中的数据是否包含逗号以及双引号,存在时,导入会失败 选择数据库 -> 右键 -> 任务 -> 导入数据 ,然后根据弹出的导入导出向导(如下图)中的提 ...
- Flask博客类登录注册验证模块代码(十四)
1 文件系统 blog #博客类 App forms #表单 __init__.py user.py models #模型 __init__.py user.py static #静态文件 templ ...
- SQL 数据匹配更新
萌新的成长之路! 最近遇到的一个需求是需要将两张关联的表的数据进行匹配(dbo.SystemUser[系统用户表]:dbo.V_Temp[系统权限视图]),即通过匹配用户ID与系统ID(拥有的系统ID ...
- Android(java)学习笔记49:通过反射获取私有构造方法并且使用
1. 反射获取私有构造方法并且使用: (1)获取字节码文件.class对象: Class c = Class.forName("cn.itcast_01.Person&qu ...