【开发新的】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());

        }

    }
相关推荐
vortex53 小时前
Apache 配置文件提权的实战思考
apache
程序员张33 小时前
Maven编译和打包插件
java·spring boot·maven
ybq195133454314 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339464 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8505 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT5 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟7 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode8 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社28 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理8 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring