BeanWrapper解析

栏目: Java · 发布时间: 6年前

  • BeanWrapper是Spring底层经常使用的一个接口,简单来说是对Bean的一种包装,包括对Bean的属性、方法,数据等。
  • 唯一的一个实现类是 BeanWrapperImpl ,继承和实现关系图如下:
    BeanWrapper解析

常用API

public BeanWrapperImpl(Object object)
public BeanWrapperImpl(Class<?> clazz)
public final Object getWrappedInstance()
public PropertyDescriptor getPropertyDescriptor(String propertyName
public PropertyDescriptor[] getPropertyDescriptors()
public Object getPropertyValue(String propertyName)

PropertyDescriptor

  • 简称属性描述器,是对属性的封装,包括属性的类型,值,get和set方法,可以通过属性描述器可以很简单的获取和修改对应的值。
  • 两个概念如下:
    ReadMethod
    WriteMethod
    

实例

  • 我们可以同 BeanWrapperPropertyDescriptor 可以很轻松实现属性的复制,下面是本人手写的一个复制的 工具 类【当然这个是很粗糙的,和BeanUtils中的copyPropreties方法不能相提并论,不喜勿喷】
    /**
     * 对象复制【浅克隆】
     */
    public class NewCopyUtils{
        private static final String CLASS="class";
    
        /**
         * 复制对象【只能复制基本数据类型,浅克隆】
         * @param source 源对象
         * @param target 目标对象
         * @param ingoreAgrs 忽略的属性
         * @throws Exception
         */
        public static void copyProperties(Object source,Object target,String... ingoreAgrs)throws Exception {
            if (source.getClass().isInterface()||target.getClass().isInterface()){
                throw new Exception("source和target不能为接口");
            }
            BeanWrapper sourceWrap = createBeanWrap(source);
            BeanWrapper targetWrap = createBeanWrap(target);
            PropertyDescriptor[] targetPds = targetWrap.getPropertyDescriptors();
            PropertyDescriptor[] sourcePds = sourceWrap.getPropertyDescriptors();
            //根据名称分组,减少一层循环
            Map<String, List<PropertyDescriptor>> map = Arrays.asList(sourcePds).parallelStream().filter(o -> !StringUtils.equals(CLASS, o.getName())).collect(Collectors.groupingBy(o -> o.getName()));
            for (int i = 0; i < targetPds.length; i++) {
                PropertyDescriptor tpd=targetPds[i];
                //去掉class属性和不要的复制的属性
                if (!StringUtils.equals(CLASS,tpd.getName())&& !Arrays.asList(ingoreAgrs).contains(tpd.getName())){
                    List<PropertyDescriptor> list = map.getOrDefault(tpd.getName(), null);
                    if (Objects.nonNull(list)){
                        Method writeMethod = tpd.getWriteMethod();
                        Method readMethod = list.get(0).getReadMethod();
                        if (Objects.isNull(writeMethod)&&Objects.isNull(readMethod)){
                            throw  new Exception("属性必须有get和set方法");
                        }
                        Object o = readMethod.invoke(source);
                        writeMethod.invoke(target,o);
                    }
                }
            }
    
        }
    
        /**
         * 构造BeanWrapImpl
         * @param source 对象
         * @return BeanWrapper
         */
        private static BeanWrapper createBeanWrap(Object source){
            return new BeanWrapperImpl(source);
        }
    }
    

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机网络

计算机网络

Andrew S. Tanenbaum / 潘爱民 / 清华大学出版社 / 2004-8-1 / 60.00元

《计算机网络(第4版)》全书按照网络协议模型(物理层、数据链路层、介质访问控制子层、网络层、传输层和应用层),自下而上系统地介绍了计算机网络的基本原理,并给出了大量实例。在讲述各网络层的同时,还融合进了近年来迅速发展起来的各种网络技术,如Internet、SONET、A DSL、CDMA、WLAN和蓝牙等。另外,针对当前计算机网络的发展现状以及计算机安全的重要性,本书用了一整章的篇幅对计算机安全进......一起来看看 《计算机网络》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具