当前位置: 首页 > news >正文

Apache BeanUtils工具介绍

beanutils,顾名思义,是java bean的一个工具类,可以帮助我们方便的读取(get)和设置(set)bean属性值、动态定义和访问bean属性;细心的话,会发现其实JDK已经提供了一个java.beans包,同样可以实现以上功能,只不过使用起来比较麻烦,所以诞生了apache commons beanutils;看源码就知道,其实apache commons beanutils就是基于jdk的java.beans包实现的。

maven:

<dependency><groupId>commons-chain</groupId><artifactId>commons-chain</artifactId><version>1.2</version>
</dependency>

commons-chain包主要有以下三个工具类:BeanUtils、PropertyUtils、ConvertUtils

1、设置、访问Bean的属性

1.1)javabean一般有以下几个特性:

1)类必须是public访问权限,且需要有一个public的无参构造方法,之所以这样主要是方便利用Java的反射动态创建对象实例:

Class beanClass = Class.forName(className);

Object beanInstance = beanClass.newInstance();

2)由于javabean的构造方法是无参的,所以我们的bean的行为配置(即设置bean的属性值)不能在构造方法完成,必须通过set方法来设置属性值。这里的setter方法会按一定的约定来命名,如setHireDate、setName。。。

3)读取和设置bean属性值的命名约定,即getter方法和setter方法,不过这里需要特别注意boolean类型的约定,如下示例:

private String firstName;
private String lastName;
private Date hireDate;
private boolean isManager;
public String getFirstName();
public void setFirstName(String firstName);
public String getLastName();
public void setLastName(String lastName);
public Date getHireDate();
public void setHireDate(Date hireDate);
public boolean isManager();
public void setManager(boolean manager);

4)并不是必须为每个属性提供setter和getter方法,我们可以只定义一个属性的getter方法而不定义setter方法,这样的属性一般是只读属性;

1.2)实战

可以通过BeanUtils和PropertyUtils设置、获取Bean的属性。

1)PropertyUtils设置、获取属性:

  • 基本数据类型:
    • PropertyUtils.getSimpleProperty(Object, String)
    • PropertyUtils.setSimpleProperty(Object, String, Object)
  • 索引类型:
    • PropertyUtils.getIndexedProperty(Object, String)
    • PropertyUtils.getIndexedProperty(Object, String, int)
    • PropertyUtils.setIndexedProperty(Object, String, Object)
    • PropertyUtils.setIndexedProperty(Object, String, int, Object)
  • Map类型:
    • PropertyUtils.getMappedProperty(Object, String)
    • PropertyUtils.getMappedProperty(Object, String, String)
    • PropertyUtils.setMappedProperty(Object, String, Object)
    • PropertyUtils.setMappedProperty(Object, String, String, Object)
  • 嵌套类型:
    • PropertyUtils.getNestedProperty(Object, String)
    • PropertyUtils.setNestedProperty(Object, String, Object)
  • 通用类型:
    • PropertyUtils.getProperty(Object, String)
    • PropertyUtils.setProperty(Object, String, Object)

示例:

//定义bean
@Data
@ToString
public class Course {private String name;private List<String> codes;private Map<String, Student> enrolledStudent = new HashMap<>();
}
@Data
@ToString
public class Student {private String name;
}//测试
Course course = new Course(); //该类必须是public的、且有默认构造方法String name = "Computer Science";
List<String> codes = Arrays.asList("CS", "CS01");//Simple Property
PropertyUtils.setSimpleProperty(course, "name", name);
PropertyUtils.setSimpleProperty(course, "codes", codes);
System.out.println(course);
String nameV = (String)PropertyUtils.getSimpleProperty(course, "name");
System.out.println(nameV);//Indexed Property
PropertyUtils.setIndexedProperty(course, "codes[1]", "CS02");
PropertyUtils.setIndexedProperty(course, "codes", 1, "CS03");
System.out.println(course);
String indexedProperty = (String)PropertyUtils.getIndexedProperty(course, "codes", 1);
String indexedProperty2 = (String)PropertyUtils.getIndexedProperty(course, "codes[1]");
System.out.println(indexedProperty + "," + indexedProperty2);Student student = new Student();
String studentName = "Joe";
student.setName(studentName);//Mapped Property
PropertyUtils.setMappedProperty(course, "enrolledStudent(ST-1)", student);
PropertyUtils.setMappedProperty(course, "enrolledStudent", "ST-1", student);
System.out.println(course);
Student mappedProperty = (Student)PropertyUtils.getMappedProperty(course, "enrolledStudent", "ST-1");
Student mappedProperty2 = (Student)PropertyUtils.getMappedProperty(course, "enrolledStudent(ST-1)");
System.out.println(mappedProperty + "," + mappedProperty2);//Nested Property
PropertyUtils.setNestedProperty(course, "enrolledStudent(ST-1).name", "Joe_1");
String nameValue = (String) PropertyUtils.getNestedProperty(course, "enrolledStudent(ST-1).name");
//等价于 String name = course.getEnrolledStudent("ST-1").getName();
System.out.println(nameValue);

以上还可以使用下面的方式:

private static void test1_1() throws Exception {Course course = new Course();String name = "Computer Science";List<String> codes = Arrays.asList("CS", "CS01");//Simple PropertyPropertyUtils.setProperty(course, "name", name);PropertyUtils.setProperty(course, "codes", codes);System.out.println(course);String nameV = (String)PropertyUtils.getProperty(course, "name");System.out.println(nameV);//Indexed PropertyPropertyUtils.setProperty(course, "codes[1]", "CS02");System.out.println(course);Student student = new Student();String studentName = "Joe";student.setName(studentName);//Mapped PropertyPropertyUtils.setProperty(course, "enrolledStudent(ST-1)", student);System.out.println(course);//Nested PropertyPropertyUtils.setProperty(course, "enrolledStudent(ST-1).name", "Joe_1");String nameValue = (String) PropertyUtils.getProperty(course, "enrolledStudent(ST-1).name");System.out.println(nameValue);
}

2)BeanUtils设置、获取属性:

BeanUtils只有以下两个方法来设置、获取属性

  • BeanUtils.getProperty(Object, String)
  • BeanUtils.setProperty(Object, String, Object)

 示例:

private static void test1_2() throws Exception {Course course = new Course();String name = "Computer Science";List<String> codes = Arrays.asList("CS", "CS01");//Simple PropertyBeanUtils.setProperty(course, "name", name);BeanUtils.setProperty(course, "codes", codes);System.out.println(course);String nameV = (String)BeanUtils.getProperty(course, "name");System.out.println(nameV);//Indexed PropertyBeanUtils.setProperty(course, "codes[1]", "CS02");System.out.println(course);Student student = new Student();String studentName = "Joe";student.setName(studentName);//Mapped PropertyBeanUtils.setProperty(course, "enrolledStudent(ST-1)", student);System.out.println(course);//Nested PropertyBeanUtils.setProperty(course, "enrolledStudent(ST-1).name", "Joe_1");String nameValue = (String) BeanUtils.getProperty(course, "enrolledStudent(ST-1).name");System.out.println(nameValue);
}

2、拷贝Bean的属性

2.1)BeanUtils有一下两个方法:

  • populate:把Map里的键值对值拷贝到bean的属性值中;
  • copyProperties:拷贝一个bean的属性到另外一个bean中,注意是浅拷贝

注意:populate和copyProperties的区别,对于Integer、Float、Boolean类型前者(populate)对于null值,在拷贝到Bean的属性后会变成默认值。

1)populate:

//bean定义
import java.util.Date;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class Employee {private Integer id;private String name;private Boolean isGood;private Float score;private Date createTime;public Employee(Integer id, String name, Boolean isGood, Float score, Date createTime) {this.id = id;this.name = name;this.isGood = isGood;this.score = score;this.createTime = createTime;}
}
//测试
Map<String, Object> map = new HashMap<>(2);
map.put("id", null);
map.put("name", "employee1");
map.put("isGood", null);
map.put("createTime", new Date()); //如果是null,会报错
map.put("add", "add1");
// map > obj
Employee e = new Employee();
BeanUtils.populate(e, map);
System.out.println(e); //Employee(id=0, name=employee1, isGood=false, createTime=Tue Aug 15 10:42:14 CST 2023)//支持类型转换
Map<String, Object> map = new HashMap<>(5);
map.put("id", "123"); 
map.put("name", 1.9);
map.put("isGood", "2"); 
map.put("score", null);
map.put("createTime", new Date());
map.put("add", "add1");
// map > obj
Employee e = new Employee();
BeanUtils.populate(e, map);
System.out.println(e); //Employee(id=123, name=1.9, isGood=false, score=1.0, createTime=Tue Aug 15 12:34:41 CST 2023)

说明:

  • 对于Integer、Float、Boolean类型,如果是null,则转成bean的属性后会变成默认值(0,0.1,false)
  • 对于Date类型,如果是null或者其他类型,转成bean的时候会报错,需要使用ConvertUtils进行类型转换(见下面)
  • 支持类型转换:比如Integer和String之间

2)copyProperties:

有另外一个对象(类型和Employee不同)

@Data
public class Employee2 {private Integer id;private String name;private String isGood;private String score;private String createTime;private Float f1;
}

测试

private static void test2_1() throws Exception {Employee ee = new Employee(null, "employee2", null, null, null);Employee2 e1 = new Employee2();BeanUtils.copyProperties(e1, ee);System.out.println(e1); //Employee2(id=null, name=employee2, isGood=null, score=null, createTime=null, f1=null)Employee ee2 = new Employee(1, "employee2", true, 4.3f, new Date());Employee2 e2 = new Employee2();BeanUtils.copyProperties(e2, ee2);System.out.println(e2); //Employee2(id=1, name=employee2, isGood=true, score=4.3, createTime=Tue Aug 15 11:45:06 CST 2023, f1=null)
}

说明:

  • 对于Integer、Float、Boolean、Date类型,如果是null,拷贝后也是null
  • 支持类型转换:两个bean只要属性名一样,类型可转换即可拷贝。比如Integer、Float、Boolean类型转成String

2.2)PropertyUtils进行属性拷贝:

1)和BeanUtils区别:

  • PropertyUtils只支持两个Bean之间的属性拷贝,不支持Map到Bean的
  • PropertyUtils在两个Bean之间进行属性拷贝时,必须要保证名字和类型都一样才行,否则会报错。BeanUtils在进行属性拷贝时,名字一样,类型可转换即可。

 

Employee ee = new Employee(null, "employee2", null, null, null);
Employee2 e1 = new Employee2();
PropertyUtils.copyProperties(e1, ee);
System.out.println(e1); //Employee2(id=null, name=employee2, isGood=null, score=null, createTime=null, f1=null)Employee ee2 = new Employee(1, "employee2", true, 4.3f, new Date());
Employee2 e2 = new Employee2();
PropertyUtils.copyProperties(e2, ee2);
System.out.println(e2); //报错

报错信息:

 

2)说明:

  • PropertyUtils不支持类型转换。
  • 对于null值,PropertyUtils拷贝后也是null

3、ConvertUtils自定义类型转换

1)DateConverter:

DateConverter继承DateTimeConverter,是beanutils包中自带的时间类型转换,包含了:

示例

DateConverter converter = new DateConverter();
converter.setPattern("yyyy-MM-dd");
ConvertUtils.register(converter, java.util.Date.class);Map<String, Object> map = new HashMap<>(5);
map.put("id", 1); 
map.put("name", "testConvertUtils");
map.put("isGood", false); //可以是Integer或String,1或"1" true 其他表示false,null表示false
map.put("score", 1.1f);
map.put("createTime", "2023-08-15");
map.put("add", "add1");// map > obj
Employee e = new Employee();
BeanUtils.populate(e, map);
System.out.println(e); //Employee(id=1, name=testConvertUtils, isGood=false, score=1.1, createTime=Tue Aug 15 00:00:00 CST 2023)

2)自定义: 

ConvertUtils.register(new Converter() {public Object convert(Class type, Object value) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");try {return simpleDateFormat.parse(value.toString());} catch (Exception e) {e.printStackTrace();}return null;}
}, Date.class);
Employee e1 = new Employee();
BeanUtils.setProperty(e1, "createTime", "2022-09-09");
System.out.println(e1.getCreateTime()); //Fri Sep 09 00:00:00 CST 2022

说明:对于setProperty,如果存在时间类型,需要自定义转换器。

相关文章:

Apache BeanUtils工具介绍

beanutils&#xff0c;顾名思义&#xff0c;是java bean的一个工具类&#xff0c;可以帮助我们方便的读取(get)和设置(set)bean属性值、动态定义和访问bean属性&#xff1b;细心的话&#xff0c;会发现其实JDK已经提供了一个java.beans包&#xff0c;同样可以实现以上功能&…...

java 原子操作 笔记

目录 java 变量原子操作 java byte[] 原子操作 java 变量原子操作 public class Counter {private int count 0;public synchronized void increment() {count;}public synchronized int getCount() {return count;} } java byte[] 原子操作 public class SharedArray {pr…...

什么是线程安全性问题?Java中有哪些常用的同步机制来解决线程安全性问题?

线程安全性问题是指在多线程环境下&#xff0c;多个线程同时访问和修改共享数据时可能引发的数据不一致、竞态条件和并发访问异常等问题。线程安全性问题的主要原因是多个线程之间的并发执行&#xff0c;导致数据的访问和修改顺序不确定&#xff0c;从而产生不一致的结果。 为…...

Gitlab 安装全流程

Version&#xff1a;gitlab-ce:16.2.4-ce.0 简介 Gitlab 是一个开源的 Git 代码仓库系统&#xff0c;可以实现自托管的 Github 项目&#xff0c;即用于构建私有的代码托管平台和项目管理系统。系统基于 Ruby on Rails 开发&#xff0c;速度快、安全稳定。它拥有与 Github 类似…...

pdf转word最简单方法~

pdf转word最简单方法&#xff01;pdf转word最简单方法我们都知道&#xff0c;PDF文件是一种只读文件格式&#xff0c;无法按照需求对PDF文件进行更改与编辑&#xff0c;从而影响到了PDF文件的使用。所以&#xff0c;我们需要将PDF文件转换为word文档&#xff0c;以此来保证文件…...

Android 9.0 WiFi 扫描结果上报和获取流程

本文是对wifi扫描结果上报和获取过程的java层代码流程梳理总结。 我们先分析扫描成功的上报和获取过程。 一、WiFi扫描成功的上报和获取过程 WiFi扫描成功的上报和获取大致是由三条不连贯流程组成的&#xff0c;分别是通知framework和WifiTracker获取扫描结果以及应用主动获取…...

Java 项目日志实例:Log4j2

点击下方关注我&#xff0c;然后右上角点击...“设为星标”&#xff0c;就能第一时间收到更新推送啦~~~ Apache Log4j 2 是对 Log4j 的升级&#xff0c;与其前身 Log4j 1.x 相比有了显着的改进&#xff0c;并提供了许多 Logback 可用的改进&#xff0c;同时支持 JCL 以及 SLF4J…...

Effective C++条款14——在资源管理类中小心coping行为(资源管理)

条款13导入这样的观念:“资源取得时机便是初始化时机”(Resource Acquisitionls Initialization; RAII)&#xff0c;并以此作为“资源管理类”的脊柱&#xff0c;也描述了auto_ ptr和tr1::shared ptr如何将这个观念表现在 heap-based资源上。然而并非所有资源都是heap-based&am…...

【网络教程】如何创建/添加钉钉机器人以及如何获取机器人的Token/Secret

文章目录 创建钉钉机器人添加钉钉机器人获取机器人的Token/Secret相关网站创建钉钉机器人 这里以PC端的操作为例,按照如下操作进行 访问 钉钉开放平台选择机器人选项卡,点击右上角的创建应用,这里会有一个弹窗,我这里选择的是继续使用旧版,如图按照要求填写相关信息创建自…...

wx原生微信小程序入门常用总结

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、定义值和修改值1、定义值2、修改值&#xff08;1&#xff09;代码&#xff08;2&#xff09;代码说明&#xff08;3&#xff09;注意点 二、点击事件三、微…...

制作一个专属于安防监控业的小程序商城

随着科技的发展和人们生活水平的提高&#xff0c;安防监控设备在我们的日常生活中起到了越来越重要的作用。因此&#xff0c;建立一个安防监控设备商城小程序就变得尤为重要。下面将介绍如何建立这样一个小程序。 第一步&#xff0c;登录乔拓云平台后台&#xff0c;进入商城管理…...

基于java羽毛球馆管理系统设计与实现

摘 要 时代的变化速度实在超出人类的所料&#xff0c;21世纪&#xff0c;计算机已经发展到各行各业&#xff0c;各个地区&#xff0c;它的载体媒介-计算机&#xff0c;大众称之为的电脑&#xff0c;是一种特高速的科学仪器&#xff0c;比人类的脑袋要灵光无数倍&#xff0c;什么…...

安装elasticsearch8.9.0及修改配置

安装es流程 打开文件,添加以下行 vim /etc/sysctl.conf vm.max_map_count=262144重启生效 sysctl -p创建用户 useradd es passwd es修改es目录所属用户 chown -R es:es /opt/elasticsearch-8.9.0如果内存不足,可以修改es的初始化内存和Max内存,修改文件/opt/elasticsearch-8…...

如何构建高效的接口自动化测试框架?看完你就会了...

在选择接口测试自动化框架时&#xff0c;需要根据团队的技术栈和项目需求来综合考虑。对于测试团队来说&#xff0c;使用Python相关的测试框架更为便捷。无论选择哪种框架&#xff0c;重要的是确保 框架功能完备&#xff0c;易于维护和扩展&#xff0c;提高测试效率和准确性。今…...

53 | 金融行业股票销售指标分析

金融行业股票销售指标分析 引言: 金融行业中的股票销售指标分析是评估股票市场表现、投资者行为以及交易平台效果的重要手段。通过深入分析关键的销售指标,投资者、金融机构和交易平台可以更好地了解市场趋势,作出明智的投资决策,优化交易策略。本文将探讨金融行业股票销售…...

qiuzhiji1

前言:记录一下毕业后的求职历程 背景:18级 湖北理工学院计算机学院(黄石) 网络工程 本文初次撰写于2023年8月17日,正处于离职找工作的空档期,部分经历可能记不清了。所有内容尽量保证了客观,主要是分享一下自己的经历,顺带锻炼文字能力。 文章会不定期更新,较新的日期会…...

使用VisualStudio制作上位机(二)

文章目录 使用VisualStudio制作上位机(二)第三部分:GUI内部函数设计使用VisualStudio制作上位机(二) Author:YAL 第三部分:GUI内部函数设计 事件添加 给窗体或窗体按钮相关的操作添加事件有两种方式,事件的名字直白的表面了这是什么事件。 直接双击界面,自动生成窗…...

Datawhale AI夏令营 - 用户新增预测挑战赛 | 学习笔记

数据分析与可视化 为了拟合出更好的结果就要了解训练数据之间的相互关系&#xff0c;进行数据分析是必不可少的一步 导入必要的库 # 导入库 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns pandas库是一个强大的分析结构化…...

HarmonyOS/OpenHarmony(Stage模型)卡片开发AbilityStage组件容器

AbilityStage是一个Module级别的组件容器&#xff0c;应用的HAP在首次加载时会创建一个AbilityStage实例&#xff0c;可以对该Module进行初始化等操作。 AbilityStage与Module一一对应&#xff0c;即一个Module拥有一个AbilityStage。 DevEco Studio默认工程中未自动生成Abilit…...

利用torchvision库实现目标检测与语义分割

一、介绍 利用torchvision库实现目标检测与语义分割。 二、代码 1、目标检测 from PIL import Image import matplotlib.pyplot as plt import torchvision.transforms as T import torchvision import numpy as np import cv2 import randomCOCO_INSTANCE_CATEGORY_NAMES …...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

视频字幕质量评估的大规模细粒度基准

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 摘要 视频字幕在文本到视频生成任务中起着至关重要的作用&#xff0c;因为它们的质量直接影响所生成视频的语义连贯性和视觉保真度。尽管大型视觉-语言模型&#xff08;VLMs&#xff09;在字幕生成方面…...

Psychopy音频的使用

Psychopy音频的使用 本文主要解决以下问题&#xff1a; 指定音频引擎与设备&#xff1b;播放音频文件 本文所使用的环境&#xff1a; Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

Swagger和OpenApi的前世今生

Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章&#xff0c;二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑&#xff1a; &#x1f504; 一、起源与初创期&#xff1a;Swagger的诞生&#xff08;2010-2014&#xff09; 核心…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

使用Matplotlib创建炫酷的3D散点图:数据可视化的新维度

文章目录 基础实现代码代码解析进阶技巧1. 自定义点的大小和颜色2. 添加图例和样式美化3. 真实数据应用示例实用技巧与注意事项完整示例(带样式)应用场景在数据科学和可视化领域,三维图形能为我们提供更丰富的数据洞察。本文将手把手教你如何使用Python的Matplotlib库创建引…...

论文笔记——相干体技术在裂缝预测中的应用研究

目录 相关地震知识补充地震数据的认识地震几何属性 相干体算法定义基本原理第一代相干体技术&#xff1a;基于互相关的相干体技术&#xff08;Correlation&#xff09;第二代相干体技术&#xff1a;基于相似的相干体技术&#xff08;Semblance&#xff09;基于多道相似的相干体…...

力扣热题100 k个一组反转链表题解

题目: 代码: func reverseKGroup(head *ListNode, k int) *ListNode {cur : headfor i : 0; i < k; i {if cur nil {return head}cur cur.Next}newHead : reverse(head, cur)head.Next reverseKGroup(cur, k)return newHead }func reverse(start, end *ListNode) *ListN…...