当前位置: 首页 > news >正文

【开发新的】apache common BeanUtils忽略null值

前言: BeanUtils默认的populate方法不会忽略空值和null值,在特定场景,我们需要原始的值避免被覆盖,所以这里提供一种自定义实现方式。

package com.hmwl.service.program;import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ContextClassLoaderLocal;import java.lang.reflect.InvocationTargetException;
import java.util.Map;/*** @Author: martin* @Date: 2023/11/01 15:13 pm* @Description: apache common 原版populate不会过滤null值,不符合使用场景*/
@Slf4j
public class CustomBeanUtils extends BeanUtilsBean {private static final ContextClassLoaderLocal<CustomBeanUtils>BEANS_BY_CLASSLOADER = new ContextClassLoaderLocal<CustomBeanUtils>() {// Creates the default instance used when the context classloader is unavailable@Overrideprotected CustomBeanUtils initialValue() {return new CustomBeanUtils();}};public static CustomBeanUtils getInstance() {return BEANS_BY_CLASSLOADER.get();}public static void setInstance(final CustomBeanUtils newInstance) {BEANS_BY_CLASSLOADER.set(newInstance);}public static void populateIgnoreEmpty(final Object bean, final Map<String, ? extends Object> properties) {try {CustomBeanUtils.getInstance().populateIgnoreNull(bean, properties);} catch (IllegalAccessException e) {log.error(e.getMessage());} catch (InvocationTargetException e) {log.error(e.getMessage());}}private final void populateIgnoreNull(final Object bean, final Map<String, ? extends Object> properties)throws IllegalAccessException, InvocationTargetException {if ((bean == null) || (properties == null)) {return;}if (log.isDebugEnabled()) {log.debug("BeanUtils.populate(" + bean + ", " +properties + ")");}for (final Map.Entry<String, ? extends Object> entry : properties.entrySet()) {final String name = entry.getKey();// 增强下,因为可能多次调用,当value为null的时候不赋值if (name == null || entry.getValue() == null) {continue;}setProperty(bean, name, entry.getValue());}}
}

原版实现:

    public void populate(final Object bean, final Map<String, ? extends Object> properties)throws IllegalAccessException, InvocationTargetException {// Do nothing unless both arguments have been specifiedif ((bean == null) || (properties == null)) {return;}if (log.isDebugEnabled()) {log.debug("BeanUtils.populate(" + bean + ", " +properties + ")");}// Loop through the property name/value pairs to be setfor(final Map.Entry<String, ? extends Object> entry : properties.entrySet()) {// Identify the property name and value(s) to be assignedfinal String name = entry.getKey();if (name == null) {continue;}// Perform the assignment for this propertysetProperty(bean, name, entry.getValue());}}

http://www.lryc.cn/news/216608.html

相关文章:

  • coalesce函数(SQL )
  • 一键报警可视对讲管理机10寸触摸屏管理机
  • java左右括号
  • 接口自动化测试 —— 工具、请求与响应
  • 【LeetCode:2103. 环和杆 | 模拟】
  • 微信小程序-授权登录(手机号码)
  • 视觉问答(VQA)12篇顶会精选论文合集,附常用数据集下载
  • 详解--编码(ASCII\Unicode,UTF-8\UTF-16\UTF-32)
  • Linux安装配置awscli命令行接口工具及其从aws上传下载数据
  • 中国联通携手华为助力长城精工启动商用5G-A柔性产线
  • 【自动化测试】Java+Selenium自动化测试环境搭建
  • 若依笔记(四):代码生成器
  • 怎样做好金融投资翻译
  • ubuntu 分区 方案
  • Python自动化测试面试题总结
  • 客户端性能测试基础知识
  • 多模态论文阅读之VLMo
  • 休闲类手游还有机会吗?两大策略收割全球玩家
  • Git复制代码
  • 数据结构笔记——查找、排序(王道408)
  • MySQL---搜索引擎
  • 2022最新版-李宏毅机器学习深度学习课程-P32 Transformer
  • 如何使用商品详情API接口获取商品数据:一篇详尽的论述
  • 华为:手机王者归来,汽车起死回生
  • Vue3.0 provide与inject依赖注入:VCA
  • 前端react入门day02-React中的事件绑定与组件
  • 工业5G路由器;小体积 千兆高速通信组网
  • 【深度学习基础】从R-CNN到Fast R-CNN,再到MaskR-CNN,发展历程讲清楚!
  • 面试算法51:节点值之和最大的路径
  • 阿里云 k8s 容器服务 设置节点为不可调度的两种方法有什么区别?