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

2023-11-03 10:15

本文主要是介绍【开发新的】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());}}

这篇关于【开发新的】apache common BeanUtils忽略null值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/337473

相关文章

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Apache Ignite缓存基本操作实例详解

《ApacheIgnite缓存基本操作实例详解》文章介绍了ApacheIgnite中IgniteCache的基本操作,涵盖缓存获取、动态创建、销毁、原子及条件更新、异步执行,强调线程池注意事项,避免... 目录一、获取缓存实例(Getting an Instance of a Cache)示例代码:二、动态

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

Spring Boot 整合 Apache Flink 的详细过程

《SpringBoot整合ApacheFlink的详细过程》ApacheFlink是一个高性能的分布式流处理框架,而SpringBoot提供了快速构建企业级应用的能力,下面给大家介绍Spri... 目录Spring Boot 整合 Apache Flink 教程一、背景与目标二、环境准备三、创建项目 & 添

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl