Apache Commons BeansUtils简介
1.概述
Apache Commons BeansUtils 包含使用 Java bean 所需的所有工具。
简单地说,bean 是一个简单的 Java 类,包含字段、getter/setter 和无参数构造函数。
Java 提供反射和自省功能来识别 getter-setter 方法并动态调用它们。但是,这些 API 可能很难学习,并且可能需要开发人员编写样板代码来执行最简单的操作。
2. Maven依赖
这是在使用它之前需要在 POM 文件中包含的 Maven 依赖项:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
最新版本可以在这里 找到。
3. 创建一个 Java Bean
让我们使用典型的 getter 和 setter 方法创建两个 bean 类Course和Student 。
public class Course {
private String name;
private List<String> codes;
private Map<String, Student> enrolledStudent = new HashMap<>();
// standard getters/setters
}
public class Student {
private String name;
// standard getters/setters
}
我们有一个具有课程名称、课程代码和多个注册学生的Course类。注册学生由唯一的注册 ID 标识。Course类在Map对象中维护注册学生,其中注册 ID 是键,学生对象将是值。
4. 属性
Bean 的属性可以分为三类。
4.1. 简单属性
单值属性也称为简单或标量。
它们的值可能是原始类型(例如 int、float)或复杂类型对象。BeanUtils 有一个PropertyUtils类,它允许我们修改 Java Bean 中的简单属性。
这是设置属性的示例代码:
Course course = new Course();
String name = "Computer Science";
List<String> codes = Arrays.asList("CS", "CS01");
PropertyUtils.setSimpleProperty(course, "name", name);
PropertyUtils.setSimpleProperty(course, "codes", codes);
4.2. 索引属性
索引属性有一个集合作为可以使用索引号单独访问的值。作为 JavaBean 的扩展,BeanUtils 也将java.util.List类型值视为索引。
我们可以使用PropertyUtils 的 setIndexedProperty方法修改索引属性的单个值。
这是修改索引属性的示例代码:
PropertyUtils.setIndexedProperty(course, "codes[1]", "CS02");
4.3. 映射属性
任何以java.util.Map作为基础类型的属性都称为映射属性。BeanUtils 允许我们使用字符串值*键更新映射中的单个值。
这是修改映射属性中的值的示例代码:
Student student = new Student();
String studentName = "Joe";
student.setName(studentName);
PropertyUtils.setMappedProperty(course, "enrolledStudent(ST-1)", student);
5. 嵌套属性访问
如果一个属性值是一个对象并且我们需要访问该对象内部的一个属性值——那将是访问一个嵌套属性。PropertyUtils也允许我们访问和修改嵌套属性。
假设我们想通过Course对象访问Student类的 name 属性。我们可能会写:
String name = course.getEnrolledStudent("ST-1").getName();
我们可以使用getNestedProperty访问嵌套属性值,并使用 PropertyUtils 中的setNestedProperty方法修改嵌套属性。这是代码:
Student student = new Student();
String studentName = "Joe";
student.setName(studentName);
String nameValue
= (String) PropertyUtils.getNestedProperty(
course, "enrolledStudent(ST-1).name");
6.复制Bean属性
将一个对象的属性复制到另一个对象对于开发人员来说通常是乏味且容易出错的。BeanUtils类提供了一个copyProperties方法,该方法将源对象的属性复制到**两个对象中属性名称相同的目标对象。
让我们创建另一个 bean 类,作为我们在上面创建的Course具有相同的属性,除了它不会有registeredStudent属性而是属性名称将是students。让我们将该类命名为CourseEntity。该类将如下所示:
public class CourseEntity {
private String name;
private List<String> codes;
private Map<String, Student> students = new HashMap<>();
// standard getters/setters
}
现在我们将Course对象的属性复制到CourseEntity对象:
Course course = new Course();
course.setName("Computer Science");
course.setCodes(Arrays.asList("CS"));
course.setEnrolledStudent("ST-1", new Student());
CourseEntity courseEntity = new CourseEntity();
BeanUtils.copyProperties(courseEntity, course);
请记住,这将仅复制具有相同名称的属性。因此,它不会复制Course类中的属性*registeredStudent,因为 CourseEntity类中没有同名的属性。