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

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

java 复制代码
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
        @Override
        protected 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());
        }
    }
}

原版实现:

java 复制代码
    public void populate(final Object bean, final Map<String, ? extends Object> properties)
        throws IllegalAccessException, InvocationTargetException {

        // Do nothing unless both arguments have been specified
        if ((bean == null) || (properties == null)) {
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("BeanUtils.populate(" + bean + ", " +
                    properties + ")");
        }

        // Loop through the property name/value pairs to be set
        for(final Map.Entry<String, ? extends Object> entry : properties.entrySet()) {
            // Identify the property name and value(s) to be assigned
            final String name = entry.getKey();
            if (name == null) {
                continue;
            }

            // Perform the assignment for this property
            setProperty(bean, name, entry.getValue());

        }

    }
相关推荐
霍田煜熙10 分钟前
【无标题】
java
无忧智库13 分钟前
深度拆解:某大型医院“十五五”智慧医院建设方案,如何冲刺互联互通五级乙等?(附技术架构与实施路径)
java·数据库·架构
守护砂之国泰裤辣22 分钟前
Windows+docker下简单kafka测试联调
java·运维·spring boot·docker·容器
代码方舟24 分钟前
Java企业级风控实战:对接天远多头借贷行业风险版API构建信贷评分引擎
java·开发语言
Maiko Star35 分钟前
Word工具类——实现导出自定义Word文档(基于FreeMarker模板引擎生成动态内容的Word文档)
java·word·springboot·工具类
优雅的38度36 分钟前
maven的多仓库配置理解
java·架构
周末吃鱼42 分钟前
研发快速使用JMeter
java·jmeter
EntyIU43 分钟前
自己实现mybatisplus的批量插入
java·后端
小途软件1 小时前
基于深度学习的人脸检测算法研究
java·人工智能·pytorch·python·深度学习·语言模型
小CC吃豆子1 小时前
Java数据结构与算法
java·开发语言