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

        }

    }
相关推荐
q***13614 分钟前
史上最厉害的Java进阶之路
java·开发语言
q***385111 分钟前
Spring Boot + Spring AI快速体验
人工智能·spring boot·spring
任子菲阳12 分钟前
学Java第四十五天——不可变集合、Stream流
java·开发语言·windows
q***483115 分钟前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
SelectDB23 分钟前
Apache Doris AI 能力揭秘(三):AI_AGG 与 EMBED 函数深度解析
数据库·后端·apache
少睡点觉27 分钟前
LeetCode 238. 除自身以外数组的乘积 问题分析+解析
java·算法·leetcode
9523638 分钟前
数据结构-二叉树
java·数据结构·学习
学IT的周星星39 分钟前
SpringMVC请求参数的绑定
java·开发语言
一 乐1 小时前
宠物猫店管理|宠物店管理|基于Java+vue的宠物猫店管理管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·后端·宠物管理
r***99821 小时前
在2023idea中如何创建SpringBoot
java·spring boot·后端