zoukankan      html  css  js  c++  java
  • springframework内BeanUtils源码使用记录一

    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

  • 相关阅读:
    异步多线程 Thread ThreadPool Task
    ASP.NET MVC IActionFilter IResultFilter IExceptionFilter/HandleError
    ASP.NET MVC Area 区域
    ASP.NET MVC Bundles 合并压缩(js css)
    ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性
    ASP.NET MVC Form表单验证与Authorize特性
    ASP.NET MVC Owin 基本理解
    CSS 基础 例子 图片拼合技术
    CSS 基础 例子 display属性:block、inline和inline-block的区别
    UVa 1326
  • 原文地址:https://www.cnblogs.com/rgqancy/p/6707410.html
Copyright © 2011-2022 走看看