BeanWrapper解析

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

  • 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);
        }
    }

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

查看所有标签

猜你喜欢:

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

Python语言程序设计基础(第2版)

Python语言程序设计基础(第2版)

嵩天、礼欣、黄天羽 / 高等教育出版社 / 2017-2 / 39

本书提出了以理解和运用计算生态为目标的Python语言教学思想,不仅系统讲解了Python语言语法,同时介绍了从数据理解到图像处理的14个Python函数库,向初学Python语言的读者展示了全新的编程语言学习路径。 全书一共设计了25个非常具有现代感的实例,从绘制蟒蛇、理解天天向上的力量到机器学习、网络爬虫,从文本进度条、统计名著人物重要性到图像手绘效果、雷达图绘制,绝大多数实例为作者原创......一起来看看 《Python语言程序设计基础(第2版)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

SHA 加密
SHA 加密

SHA 加密工具

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

正则表达式在线测试