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一次,再次存入缓存中.此时需 ...
随机推荐
- struts quick start
1.create a dynamic web project 2.import the needed jar(about 11) 3. request page(index.jsp) <%@ p ...
- Python进阶篇四:Python文件和流
摘要: Python对于文件和流的操作与其他编程语言基本差不多,甚至语句上比其他语言更为简洁.文件和流函数针对的对象除了这两者之外还有,类文件(file-like),即python中只支持读却不支持写 ...
- 【Gradle】 Gradle 综合
Gradle User Guide:http://www.gradle.org/docs/current/userguide/userguide.html 针对它的中文翻译:http://ask.an ...
- uwsgi特性
uwsgi 特性 官网参考 X-Sendfile仿真 即使前端 代理/webserver 不支持X-Sendfile (或者不能访问静态资源),可以使用 uwsgi 内部的 offloading 来模 ...
- java面试题全集(上)--java基础
本文转载自:https://blog.csdn.net/jackfrued/article/details/44921941 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - ...
- Flask入门模板Jinja2语法与函数(四)
1 模板的创建 模板文件结构: project/ templates/ 模板文件 跳转模板一般使用: from flask import render_template,render_template ...
- 二、安装桌面——Linux学习笔记
安装桌面并不是商业化用的,只是为了熟悉Linux文档结构而已. 这个比较简单 1.输入安装桌面命令 # yum -y groups install "GNOME Desktop" ...
- 【Leetcode】【Easy】Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- HttpClient拉取连载小说
上午刚入手的小说,下午心血来潮想从网站上拉取下来做成电子书,呵呵,瞎折腾-说做就做- [抓包] 这一步比什么都重要,如果找不到获取真正资源的那个请求,就什么都不用做了- 先是打算用迅雷把所有页面都下载 ...
- myeclipse 阿里巴巴java开发规范
今天发现阿里巴巴搞了个java开发规范,然后火了: 然后发现我也整了一下: 阿里巴巴java开发规范宣传页: http://mp.weixin.qq.com/s/IbibsXlWHlM59kfXJqR ...