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

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

查看所有标签

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

How to Solve It

How to Solve It

Zbigniew Michalewicz、David B. Fogel / Springer / 2004-03-01 / USD 59.95

This book is the only source that provides comprehensive, current, and detailed information on problem solving using modern heuristics. It covers classic methods of optimization, including dynamic pro......一起来看看 《How to Solve It》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换