1. Overview
1.概述
Apache Commons BeansUtils contains all tools necessary for working with Java beans.
Apache Commons BeansUtils包含了处理Java Bean的所有必要工具。
Simply put, a bean is a simple Java classes containing fields, getters/setters, and a no-argument constructor.
简单地说,Bean是一个简单的Java类,包含字段、getters/setters和一个无参数的构造函数。
Java provides reflection and introspection capabilities to identify getter-setter methods and call them dynamically. However, these APIs can be difficult to learn and may require developers to write boilerplate code to perform simplest operations.
Java提供了反射和自省功能,以识别getter-setter方法并动态地调用它们。然而,这些API可能很难学习,而且可能需要开发人员编写模板代码来执行最简单的操作。
2. Maven Dependencies
2.Maven的依赖性
Here is the Maven dependency need to be included in the POM file before using it:
在使用前,需要在POM文件中包含Maven依赖。
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
The newest version can be found here.
最新的版本可以在这里找到。
3. Creating a Java Bean
3.创建一个Java Bean
Let’s create two bean classes Course and Student with typical getter and setter methods.
让我们创建两个带有典型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
}
We have a Course class that has a course name, course codes and multiple enrolled students. Enrolled Students are identified by unique enrollment Id. Course class maintains enrolled students in a Map object where enrollment Id is a key, and the student object will be the value.
我们有一个课程类,有一个课程名称、课程代码和多个注册学生。注册的学生是由唯一的注册ID识别的。Course类在一个Map对象中维护注册学生,其中enrollment Id是一个键,学生对象将是值。
4. Property Access
4.财产访问
Bean properties can be divided into three categories.
Bean属性可分为三类。
4.1. Simple Property
4.1.简单属性
Single-value properties are also called simple or scalar.
单值属性也被称为简单或标量。
Their value might be a primitive (such as int, float) or complex type objects. BeanUtils has a PropertyUtils class that allows us to modify simple properties in a Java Bean.
它们的值可能是一个原始的(如int、float)或复杂类型的对象。BeanUtils有一个PropertyUtils类,允许我们修改Java Bean中的简单属性。
Here is the example code to set the properties:
下面是设置属性的示例代码。
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. Indexed Property
4.2.指数化财产
Indexed properties have a collection as a value that can be individually accessed using an index number. As an extension to JavaBean, BeanUtils considers java.util.List type values as indexed as well.
索引属性有一个集合作为一个值,可以使用一个索引号单独访问。作为JavaBean的扩展,BeanUtils认为java.util.List类型的值也是索引的。
We can modify an indexed property individual value using a PropertyUtils’s setIndexedProperty method.
我们可以使用PropertyUtils的setIndexedProperty方法来修改一个索引属性的单独值。
Here is example code modifying indexed property:
下面是修改索引属性的示例代码。
PropertyUtils.setIndexedProperty(course, "codes[1]", "CS02");
4.3. Mapped Property
4.3.映射属性
Any property that has a java.util.Map as the underlying type is called a mapped property. BeanUtils allows us to update the individual value in a map using a String-valued key.
任何以java.util.Map为基础类型的属性都被称为映射属性。BeanUtils允许我们使用一个String-valued键来更新map中的单个值。
Here is the example code to modify the value in a mapped property:
下面是修改映射属性中的值的示例代码。
Student student = new Student();
String studentName = "Joe";
student.setName(studentName);
PropertyUtils.setMappedProperty(course, "enrolledStudent(ST-1)", student);
5. Nested Property Access
5.嵌套属性访问
If a property value is an object and we need to access a property value inside that object – that would be accessing a nested property. PropertyUtils allow us to access and modify nested properties as well.
如果一个属性值是一个对象,而我们需要访问这个对象中的一个属性值–这将是访问一个嵌套的属性。PropertyUtils允许我们访问和修改嵌套属性。
Assume we want to access the name property of Student class through Course object. We might write:
假设我们想通过Course对象访问Student类的name属性。我们可以这样写
String name = course.getEnrolledStudent("ST-1").getName();
We can access the nested property values using getNestedProperty and modify the nested property using setNestedProperty methods in PropertyUtils. Here is the code:
我们可以使用getNestedProperty访问嵌套的属性值,使用PropertyUtils中的setNestedProperty方法修改嵌套的属性。下面是代码。
Student student = new Student();
String studentName = "Joe";
student.setName(studentName);
String nameValue
= (String) PropertyUtils.getNestedProperty(
course, "enrolledStudent(ST-1).name");
6. Copy Bean Properties
6.复制Bean属性
Copying properties of one object to another object is often tedious and error-prone for developers. BeanUtils class provides a copyProperties method that copies the properties of source object to target object where the property name is same in both objects.
对于开发者来说,将一个对象的属性复制到另一个对象上往往是繁琐而容易出错的。BeanUtils类提供了一个copyProperties方法,可以将源对象的属性复制到目标对象,其中两个对象的属性名称相同。
Let’s create another bean class as Course we created above with same properties except it will not have enrolledStudent property instead property name will be students. Let’s name that class CourseEntity. The class would look like:
让我们创建另一个与我们上面创建的Course相同属性的bean类,除了它将没有enrolledStudent属性,而属性名称将是students。让我们把这个类命名为CourseEntity。这个类看起来会是这样。
public class CourseEntity {
private String name;
private List<String> codes;
private Map<String, Student> students = new HashMap<>();
// standard getters/setters
}
Now we will copy the properties of Course object to CourseEntity object:
现在我们将把Courseobject的属性复制到CourseEntityobject。
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);
Remember this will copy the properties with the same name only. Therefore, it will not copy the property enrolledStudent in Course class because there is no property with the same name in CourseEntity class.
记住这将只复制具有相同名称的属性。因此,它不会复制Course类中的属性enrolledStudent,因为在CourseEntity类中没有相同名称的属性。
7. Conclusion
7.结论
In this quick article, we went over the utility classes provided by BeanUtils. We also looked into different types of properties and how can we access and modify their values.
在这篇快速文章中,我们了解了BeanUtils提供的实用类。我们还研究了不同类型的属性,以及我们如何访问和修改它们的值。
Finally, we looked into accessing nested property values and copying properties of one object to another object.
最后,我们研究了访问嵌套属性值和将一个对象的属性复制到另一个对象。
Of course, reflection and introspection capabilities in the Java SDK also allow us to access properties dynamically but it can be difficult to learn and require some boilerplate code. BeanUtils allows us to access and modify these values with a single method call.
当然,Java SDK中的反射和自省功能也允许我们动态地访问属性,但这可能很难学习,而且需要一些模板代码。BeanUtils允许我们通过一个方法调用来访问和修改这些值。
Code snippets can be found over on GitHub.
代码片段可以在GitHub上找到over。