benym的知识笔记 benym的知识笔记
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)

benym

惟其艰难,才更显勇毅🍂惟其笃行,才弥足珍贵
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)
  • Rpamis

    • Utils

      • 无惧性能烦恼-12款Bean拷贝工具压测大比拼
      • Bean工具类-RpamisBeanUtils
        • 背景
        • 为什么需要弱引用?
        • 基本使用
    • Exception

      • MethodHandle结合LambdaMetafactory-使用方法及性能测试
      • 优雅的参数校验与全局异常-代码规范的天生落地
      • 异常工具类-ExceptionFactory
      • 异常工具类-Assert
    • Security

      • Rpamis-security-基于Mybatis-Plugin的一站式加解密脱敏安全组件
      • Rpamis-security-技术背景
      • Rpamis-security-原理解析
  • 开源项目
  • Rpamis
  • Utils
benym
2022-11-25
目录

Bean工具类-RpamisBeanUtils

# 背景

当项目中有需要使用拷贝类时,开发者可能会需要将目前的拷贝工具进行二次封装用于适配项目,减少动态创建拷贝工具的性能损耗,这里给出一个简单的封装Bean工具类。

采用并发安全、且弱引用的ConcurrentReferenceHashMap+BeanCopier的形式快速开发出自己的拷贝工具。

# 为什么需要弱引用?

当考虑为BeanCopier做实例缓存时,通常会用到ConcurrentHashMap等并发安全的Map工具,在第一次进行拷贝时会将source和target创建的拷贝实例放入map中。

在系统长期运行的情况下,内存是非常宝贵的资源,拷贝场景涉及多、使用高频,如果只是强引用的Map,则GC无法进行回收,有些低频使用的拷贝实例将得不到释放。在Java中有WeakHashMap提供弱引用的帮助,但却没有弱引用+并发安全的Map。

Spring基于这一缺口开发出了ConcurrentReferenceHashMap,提供了GC友好、且并发安全的Map工具,这也是本工具类选择它的原因。

# 基本使用

  • RpamisBeanUtils.copy(Object source, Object target)

    拷贝source对象属性到target中,名字和类型不严格匹配时将不拷贝,无返回值

EntitySource entitySource = init();
EntityTarget entityTarget = new EntityTarget();
RpamisBeanUtils.copy(entitySource, entityTarget);
1
2
3
1
2
3
  • RpamisBeanUtils.copy(Object source, Class<T> clazz)

    拷贝source对象属性到target class中,返回target

EntitySource entitySource = init();
EntityTarget entityTarget = RpamisBeanUtils.copy(entitySource, EntityTarget.class);
1
2
1
2
  • RpamisBeanUtils.copy(Object source, Object target, Converter converter)

拷贝source对象属性到target中,使用自定义converter,拷贝规则严格符合converter规则,无返回值

EntitySource entitySource = init();
EntityTargetDiff entityTargetDiff = new EntityTargetDiff();
RpamisBeanUtils.copy(entitySource, entityTargetDiff, new DiffConverter());
1
2
3
1
2
3
  • RpamisBeanUtils.copy(Object source, Class<T> clazz, Converter converter)

拷贝source对象属性到target class中,使用自定义converter,拷贝规则严格符合converter规则,返回target

EntitySource entitySource = init();
EntityTargetDiff entityTargetDiff = RpamisBeanUtils.copy(entitySource, EntityTargetDiff.class, new DiffConverter());
1
2
1
2
  • RpamisBeanUtils.copyToList(List<?> sources, Class<T> clazz)

拷贝源list对象到新class list,返回List<Target>

List<EntitySource> list = initList();
List<EntityTarget> entityTargets = RpamisBeanUtils.copyToList(list, EntityTarget.class);
1
2
1
2
  • RpamisBeanUtils.copyToList(List<?> sources, Class<T> clazz, Converter converter)

拷贝源list对象到新class list,使用自定义converter,返回List<Target>

List<EntitySource> list = initList();
List<EntityTargetDiff> entityTargetDiffs = RpamisBeanUtils.copyToList(list, EntityTargetDiff.class, new DiffConverter());
1
2
1
2
  • RpamisBeanUtils.toPageResponse(Page<T> page)

将Mybatis-plus Page对象转化为PageResponse,返回PageResponse<Source>

Page<EntitySource> page = initPage();
PageResponse<EntitySource> entitySourcePageResponse = RpamisBeanUtils.toPageResponse(page);
1
2
1
2
  • RpamisBeanUtils.toPageResponse(PageResponse<S> sourcePageResponse, Class<T> clazz)

将PageResponse对象,转化内部class,返回PageResponse<Target>

Page<EntitySource> page = initPage();
PageResponse<EntitySource> entitySourcePageResponse = RpamisBeanUtils.toPageResponse(page);
PageResponse<EntityTarget> entityTargetPageResponse = RpamisBeanUtils.toPageResponse(entitySourcePageResponse, EntityTarget.class);
1
2
3
1
2
3
  • RpamisBeanUtils.toPageResponse(Page<S> page, Function<S, T> functionConverter)

将Mybatis-plus Page对象转化为PageResponse, Function converter形式,返回PageResponse<Target>

Page<EntitySource> page = initPage();
PageResponse<EntityTarget> entityTargetPageResponse = RpamisBeanUtils.toPageResponse(page, source -> RpamisBeanUtils.copy(source, EntityTarget.class));
1
2
1
2
  • RpamisBeanUtils.toPageResponse(PageResponse<S> sourcePageResponse, Function<S, T> functionConverter)

将PageResponse对象 转换不同内部class, Function converter形式,返回PageResponse<Target>

Page<EntitySource> page = initPage();
PageResponse<EntitySource> entitySourcePageResponse = RpamisBeanUtils.toPageResponse(page);
PageResponse<EntityTargetDiff> pageResponse = RpamisBeanUtils.toPageResponse(entitySourcePageResponse,
        source -> RpamisBeanUtils.copy(source, EntityTargetDiff.class, new DiffConverter()));
1
2
3
4
1
2
3
4
编辑 (opens new window)
#开源项目#Rpamis#工具类#BeanUtil#RpamisBeanUtils
上次更新: 2023/11/26, 20:51:50
无惧性能烦恼-12款Bean拷贝工具压测大比拼
MethodHandle结合LambdaMetafactory-使用方法及性能测试

← 无惧性能烦恼-12款Bean拷贝工具压测大比拼 MethodHandle结合LambdaMetafactory-使用方法及性能测试→

最近更新
01
SpringCache基本配置类
05-16
02
DSTransactional与Transactional事务混用死锁场景分析
03-04
03
Rpamis-security-原理解析
12-13
更多文章>
Theme by Vdoing | Copyright © 2018-2024 benym | MIT License
 |   |   | 
渝ICP备18012574号 | 渝公网安备50010902502537号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式