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

        }

    }
相关推荐
zeijiershuai7 分钟前
SpringBoot项目配置文件、yml配置文件
java·spring boot·yml
Java永无止境41 分钟前
JAVASE:常见的算法
java·开发语言·数据结构·算法·排序算法
2301_8153577041 分钟前
Maven:在原了解基础上对pom.xml文件进行详细解读
java·开发语言·数据库
CrazyClaz42 分钟前
Java核心基础
java
琢磨先生David1 小时前
《Java 单例模式:从类加载机制到高并发设计的深度技术剖析》
java·设计模式
钢铁男儿1 小时前
C# 深入理解类(析构函数和this关键字)
java·python·c#
举一个梨子zz1 小时前
Java—— IO流的应用
java·开发语言·intellij-idea·io·需求分析·file
星辰离彬1 小时前
5.Java 面向对象编程入门:类与对象的创建和使用
java·开发语言·后端
yaoxin5211232 小时前
85. Java Record 深入解析:构造函数、访问器、序列化与实际应用
java·开发语言