package org.springframework.beans;

public abstract class BeanUtils

    /**
* Copy the property values of the given source bean into the given target bean.
* <p>Note: The source and target classes do not have to match or even be derived
* from each other, as long as the properties match. Any bean properties that the
* source bean exposes but the target bean does not will silently be ignored.
* @param source the source bean
* @param target the target bean
* @param editable the class (or interface) to restrict property setting to
* @param ignoreProperties array of property names to ignore
* @throws BeansException if the copying failed
* @see BeanWrapper
*/
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException { Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}

我们会遇到场景:将某个Bean实例的字段全部或者拷贝到另一个Bean的实例中。

如果是项目没有使用Spring框架,我们只能免为其难,自己写了(当然我们可以把这套代码拷贝过去^_^)[貌似以前没用过Spring时,也不读Spring源码,自己用C#真写过,独读源码很重要!!!]

另外,如果字段比较多(假如几十个),用上述这种反射的方法,比用正则表达式生成的

target.setPropertie1(source.getPropertie1());

target.setPropertie2(source.getPropertie2());

......

效率会降低吗?有时间测试比较一下。

JEECG框架提供了一套上述机制。

另外,通过字节码优化,代替反射机制,则效率更高。与PowerBuilder调用C语言(大学老师这么搞过),C#调用VC++甚至字节码汇编,基本属于同样的原理。

Python数据计算仍然是调用matlab等库。

https://blog.csdn.net/w05980598/article/details/79134379

springframework内BeanUtils源码使用记录一的更多相关文章

  1. 分析jQuery源码时记录的一点感悟

    分析jQuery源码时记录的一点感悟      1.  链式写法      这是jQuery语法上的最大特色,也许该改改POJO里的set方法,和其他的非get方法什么的,可以把多行代码合并,减去每次 ...

  2. EventBus源码解析 源码阅读记录

    EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...

  3. java内置线程池ThreadPoolExecutor源码学习记录

    背景 公司业务性能优化,使用java自带的Executors.newFixedThreadPool()方法生成线程池.但是其内部定义的LinkedBlockingQueue容量是Integer.MAX ...

  4. org.springframework.core.io包内的源码分析

    前些日子看<深入理解javaweb开发>时,看到第一章java的io流,发觉自己对io流真的不是很熟悉.然后看了下JDK1.7中io包的一点点代码,又看了org.springframewo ...

  5. hashMap源码学习记录

    hashMap作为java开发面试最常考的一个题目之一,有必要花时间去阅读源码,了解底层实现原理. 首先,让我们看看hashMap这个类有哪些属性 // hashMap初始数组容量 static fi ...

  6. java io 源码研究记录(一)

    Java IO 源码研究: 一.输入流 1  基类 InputStream 简介: 这是Java中所有输入流的基类,它是一个抽象类,下面我们简单来了解一下它的基本方法和抽象方法. 基本方法: publ ...

  7. underscore源码阅读记录

    这几天有大神推荐读underscore源码,趁着项目测试的空白时间,看了一下. 整个underscore包括了常用的工具函数,下面以1.3.3源码为例分析一下. _.size = function(o ...

  8. Tomcat源码学习记录--web服务器初步认识

    Tomcat作为开源的轻量级WEB服务器,虽然不是很适合某些大型项目,但是它开源,读其源代码可以很好的提高我们的编程功底和设计思维.Tomcat中用到了很多比较好的设计模式,其中代码风格也很值得我们去 ...

  9. 干货:Java多线程详解(内附源码)

      线程是程序执行的最小单元,多线程是指程序同一时间可以有多个执行单元运行(这个与你的CPU核心有关). 在java中开启一个新线程非常简单,创建一个Thread对象,然后调用它的start方法,一个 ...

随机推荐

  1. ROS教程2 编写简单的消息发布器和订阅器 (C++ catkin)

    创建工作环境 创建文件夹,创建包 mkdir -p ~/catkin_arduino_ros/src catkin_create_pkg test1_pub_sub std_msgs rospy ro ...

  2. Codeforces Round #553 (Div. 2) D. Stas and the Queue at the Buffet 贪心+公式转化

    题意 给出n个pair (a,b) 把它放在线性序列上 1--n 上 使得  sum(a*(j-1)+b*(n-j))  最小 思路 :对式子进行合并 同类项 有:    j*(a-b)+  (-a+ ...

  3. ls操作总结

    la = ls -a ll = ls -l ls -lsh: ls -lsh train_log 看train_log的软链接的原始路径

  4. RF

    大家在日常工作中常常解除RF模块,RF的意思就是:radio frequency的意思,就是无线电波频率的意思. RF射频范围:300KHz-300GHz; 其中小于1000HZ以内的当然是低频: 大 ...

  5. 学习CSS布局 - max-width

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 认识ASP.NET Windows身份认证

    本文摘自:细说ASP.NET Windows身份认证 Forms身份认证虽然使用广泛,不过,如果是在 Windows Active Directory 的环境中使用ASP.NET, 那么使用Windo ...

  7. webstorm破解汉化

    一.下载webstorm软件和汉化包 webstorm安装包下载链接 : 链接: https://pan.baidu.com/s/1VmOPNVL2GRgAb_0tAJhy8A 密码: am7e 汉化 ...

  8. Zookeeper-基本概念

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务.它是一个典型的分布式数据一致性的解决方案,分布式应用可以基于它实现诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知.集群管理 ...

  9. Nginx 反向代理 上传大文件报 413

    Nginx 中上传文件限制是 2m,上传太大就报错,配置一下 client_max_body_size 1024m; 就可以上传 1G 大小文件 添加在 location 中,如果是反向代理就添加在反 ...

  10. cmake 添加头文件目录,链接动态、静态库(转载)

    来源网址:http://www.cnblogs.com/binbinjx/p/5626916.html 罗列一下cmake常用的命令. CMake支持大写.小写.混合大小写的命令. 1. 添加头文件目 ...