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

相关文章

忽略某些文件 —— Git 学习笔记 05

忽略某些文件 忽略某些文件 通过.gitignore文件其他规则源如何选择规则源参考资料 对于某些文件,我们不希望把它们纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。通常它们都是些自动生成的文件,比如日志文件、编译过程中创建的临时文件等。 通过.gitignore文件 假设我们要忽略 lib.a 文件,那我们可以在 lib.a 所在目录下创建一个名为 .gi

Apache Tiles 布局管理器

陈科肇 =========== 1.简介 一个免费的开源模板框架现代Java应用程序。  基于该复合图案它是建立以简化的用户界面的开发。 对于复杂的网站,它仍然最简单,最优雅的方式来一起工作的任何MVC技术。 Tiles允许作者定义页面片段可被组装成在运行一个完整的网页。  这些片段,或Tiles,可以用于为了降低公共页面元素的重复,简单地包括或嵌入在其它瓦片,制定了一系列可重复使用

Apache HttpClient使用详解

转载地址:http://eksliang.iteye.com/blog/2191017 Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。因此熟

开源Apache服务器安全防护技术精要及实战

Apache 服务简介   Web服务器也称为WWW服务器或HTTP服务器(HTTPServer),它是Internet上最常见也是使用最频繁的服务器之一,Web服务器能够为用户提供网页浏览、论坛访问等等服务。   由于用户在通过Web浏览器访问信息资源的过程中,无须再关心一些技术性的细节,而且界面非常友好,因而Web在Internet上一推出就得到了爆炸性的发展。现在Web服务器已

Cannot read property ‘length‘ of null while opening vscode terminal

同一问题地址:Cannot read property ‘length’ of null while opening vscode terminal 问题描述 One day, 我在ubuntu 18.04下用vscode打开一个项目,并想和往常一样在vscode使用终端,发现报错Cannot read property 'length' of null。 解决 打开setting.jso

非空约束(Not Null)

修改表添加非空约束 使用DDL语句添加非空约束 ALTER TABLE 表名 MODIFY 列名 类型 NOT NULL; 示例: 向emp表中的salary添加非空约束。 alter table emp modify salary float(8,2) not NULL; 删除非空约束 使用DDL语句删除非空约束 ALTER TABLE 表名 MODIFY 列名 类型 NULL;

BeanUtils.copyProperties()在不同包下,用法不同!!! 切记!!!

用法一: 在import org.springframework.beans.BeanUtils;包下: <span style="white-space:pre"> </span>//赋值vo对象的值到po中 <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span>* <spa

Java中WebService接口的生成、打包成.exe、设置成Windows服务、及其调用、Apache CXF调用

一、Java中WebService接口的生成: 1、在eclipse工具中新建一个普通的JAVA项目,新建一个java类:JwsServiceHello.java package com.accord.ws;import javax.jws.WebMethod;import javax.jws.WebService;import javax.xml.ws.Endpoint;/*** Ti

【虚拟机/服务器】XAMPP错误: Apache shutdown unexpectedly解决办法

XAMPP安装好之后启动,但有些用户在启动apache的时候会出现: 11:41:38 [Apache] Status change detected: running11:41:38 [Apache] Status change detected: stopped11:41:38 [Apache] Error: Apache shutdown unexpectedly.11:41:38

FORM的ENCTYPE=multipart/form-data 时request.getParameter()值为null问题的解决

此情况发生于前台表单传送至后台java servlet处理: 问题:当Form需要FileUpload上传文件同时上传表单其他控件数据时,由于设置了ENCTYPE=”multipart/form-data” 属性,后台request.getParameter()获取的值为null 上传文件的参考代码:http://www.runoob.com/jsp/jsp-file-uploading.ht