BeanUtils.copyProperties and PropertyUtils.copyProperties
Both tool classes process properties with the same name that previously existed in two beans, whether it is the source bean or the target bean. None of the attributes are processed.
The principle is to use the JDK's own reflection mechanism to dynamically get and set, thereby converting our classes.
But we should pay attention to the data types they support. Another thing is that if a class is written inside a class, which is generally called an inner class, conversion of such a class will not be successful.
The biggest difference between the two is:
1.BeanUtils.copyProperties will perform type conversion, but PropertyUtils.copyProperties will not.
Since type conversion is performed, BeanUtils.copyProperties is not as fast as PropertyUtils.copyProperties.
Therefore, the scope of application of PropertyUtils.copyProperties is slightly narrower. It only copies properties with the same name and type. If the name is the same but the type is different, it will report an error.
2. Handling of null: PropertyUtils supports null scenarios; BeanUtils does not support null scenarios for some properties, specifically as follows:
1), date type is not supported ;
2), Boolean, Ineger, Long, Short, Float, Double, etc. are not supported: Convert to false, 0;
3), string: supported, keep null;
There are several things to note when using BeanUtils:
1. For attributes of type Boolean/Short/Integer/Float/Double, it will be converted to false, 0:
1 public class User { 2 3 private Integer intVal; 4 5 private Double doubleVal; 6 7 private Short shortVal; 8 9 private Long longVal; 10 11 private Float floatVal; 12 13 private Byte byteVal; 14 15 private Boolean booleanVal; 16 } 17 18 User src = new User(); 19 User dest = new User(); 20 BeanUtils.copyProperties(dest, src); 21 System.out.println(src); 22 System.out.println(dest); 23 24 //输出结果: 25 User [intVal=null, doubleVal=null, shortVal=null, longVal=null, floatVal=null, byteVal=null, booleanVal=null] 26 User [intVal=0, doubleVal=0.0, shortVal=0, longVal=0, floatVal=0.0, byteVal=0, booleanVal=false]
The explanation is that these types have corresponding basic types. When performing type conversion, it is possible When encountering a conversion similar to Integer -> int, it is obvious that the attribute of type int cannot be assigned a value of null, so it is uniformly converted to 0.
How to prevent it from turning to 0? It can be like this:


1 import org.apache.commons.beanutils.converters.IntegerConverter; 2 3 IntegerConverter converter = new IntegerConverter(null); //默认为null,而不是0 4 BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); 5 beanUtilsBean.getConvertUtils().register(converter, Integer.class);
2. For java.util.Date/BigDecimal/java.sql For the .Date/java.sql.Timestamp/java.sql.Time classes, if the value is null, an exception will be thrown during copying, and the corresponding Conveter needs to be used:
1 public class User2 { 2 3 private java.util.Date javaUtilDateVal; 4 5 private java.sql.Date javaSqlDateVal; 6 7 private java.sql.Timestamp javaSqlTimeStampVal; 8 9 private BigDecimal bigDecimalVal; 10 11 private java.sql.Time javaSqlTime; 12 13 } 14 15 User2 src = new User2(); 16 User2 dest = new User2(); 17 18 BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); 19 20 //如果没有下面几行,则在转换null时会抛异常,例如:org.apache.commons.beanutils.ConversionException: No value specified for 'BigDecimal' 21 //在org.apache.commons.beanutils.converters这个包下面有很多的Converter,可以按需要使用 22 beanUtilsBean.getConvertUtils().register(new BigDecimalConverter(null), BigDecimal.class); 23 beanUtilsBean.getConvertUtils().register(new DateConverter(null), java.util.Date.class); 24 25 beanUtilsBean.getConvertUtils().register(new SqlTimestampConverter(null), java.sql.Timestamp.class); 26 beanUtilsBean.getConvertUtils().register(new SqlDateConverter(null), java.sql.Date.class); 27 beanUtilsBean.getConvertUtils().register(new SqlTimeConverter(null), java.sql.Time.class); 28 29 beanUtilsBean.copyProperties(dest, src); 30 System.out.println(src); 31 System.out.println(dest);
Assume it is copied from A to B:
Requirement 1: If a field in B has a value (not null), then the field is not copied; that is, When the field in B has no value, it will be copied, which is suitable for supplementing the value of B.


1 import org.apache.commons.beanutils.BeanUtilsBean; 2 import org.apache.commons.beanutils.PropertyUtils; 3 4 public class CopyWhenNullBeanUtilsBean extends BeanUtilsBean{ 5 6 @Override 7 public void copyProperty(Object bean, String name, Object value) 8 throws IllegalAccessException, InvocationTargetException { 9 try { 10 Object destValue = PropertyUtils.getSimpleProperty(bean, name); 11 if (destValue == null) { 12 super.copyProperty(bean, name, value); 13 } 14 } catch (NoSuchMethodException e) { 15 throw new RuntimeException(e); 16 } 17 } 18 19 }
Requirement 2: If a field in A has no value (is null), then the field will not be copied, that is, do not copy null is copied to B.
1 import org.apache.commons.beanutils.BeanUtilsBean; 2 3 public class CopyFromNotNullBeanUtilsBean extends BeanUtilsBean { 4 5 @Override 6 public void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException { 7 if (value == null) { 8 return; 9 } 10 super.copyProperty(bean, name, value); 11 } 12 }
The above is the detailed content of Detailed explanation of Apache's object replication example tutorial. For more information, please follow other related articles on the PHP Chinese website!

本文给大家介绍如何安装apache2.4,以及如何配置php8.0,文中附有图文详细步骤,下面就带大家一起看看怎么安装配置apache2.4+php8.0吧~

mod_limitipconn,这个是apache的一个非官方模块,根据同一个来源ip进行并发连接控制,bw_mod,它可以根据来源ip进行带宽限制,它们都是apache的第三方模块。1.下载:wgetwget2.安装#tar-zxvfmod_limitipconn-0.22.tar.gz#cdmod_limitipconn-0.22#vimakefile修改:apxs=“/usr/local/apache2/bin/apxs”#这里是自己apache的apxs路径,加载模块或者#/usr/lo

查看apache版本的步骤:1、进入cmd命令窗口;2、使用cd命令切换到Apache的bin目录下,语法“cd bin目录路径”;3、执行“httpd -v”命令来查询版本信息,在输出结果中即可查看apache版本号。

本篇文章给大家带来了关于PHP的相关知识,其中主要跟大家分享在Ubuntu20.04 LTS环境下安装Apache的全过程,并且针对其中可能出现的一些坑也会提供解决方案,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

1.Nginx和tomcat的区别nginx常用做静态内容服务和代理服务器,直接外来请求转发给后面的应用服务器(tomcat,Django等),tomcat更多用来做一个应用容器,让javawebapp泡在里面的东西。严格意义上来讲,Apache和nginx应该叫做HTTPServer,而tomcat是一个ApplicationServer是一个Servlet/JSO应用的容器。客户端通过HTTPServer访问服务器上存储的资源(HTML文件,图片文件等),HTTPServer是中只是把服务器

在使用 PHP 进行网站开发时,你可能会遇到字符编码问题。特别是在使用不同的 Web 服务器时,会发现 IIS 和 Apache 处理字符编码的方法不同。当你使用 IIS 时,可能会发现在使用 UTF-8 编码时出现了乱码现象;而在使用 Apache 时,一切正常,没有出现任何问题。这种情况应该怎么解决呢?

Pacemaker是适用于类Linux操作系统的高可用性集群软件。Pacemaker被称为“集群资源管理器”,它通过在集群节点之间进行资源故障转移来提供集群资源的最大可用性。Pacemaker使用Corosync进行集群组件之间的心跳和内部通信,Corosync还负责集群中的投票选举(Quorum)。先决条件在我们开始之前,请确保你拥有以下内容:两台RHEL9/8服务器RedHat订阅或本地配置的仓库通过SSH访问两台服务器root或sudo权限互联网连接实验室详情:服务器1:node1.exa

快速查看服务器软件的编译参数:1、nginx编译参数:your_nginx_dir/sbin/nginx-v2、apache编译参数:catyour_apache_dir/build/config.nice3、php编译参数:your_php_dir/bin/php-i|grepconfigure4、mysql编译参数:catyour_mysql_dir/bin/mysqlbug|grepconfigure以下是完整的实操例子:查看获取nginx的编译参数:[root@www~]#/usr/lo


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use
