Java introspection instance parsing

The number of contours in the image. The size of the vector represents the number of points on the contour. Understanding JavaBeans

Introspector is the English word corresponding to introspection. It is mainly used to operate JavaBeans. JavaBeans are a special Java class, in which some methods comply with some naming rules. If some methods in a Java class comply with some naming rules, they can be used as JavaBeans.

JavaBean is a special Java class, which is mainly used to transfer data information. The methods in this Java class are mainly used to access private fields, and the method name conforms to some naming rules.

If you want to transfer multiple information between two modules, you can encapsulate these information into a JavaBean. The instance object of this JavaBean is usually called value object (VO). This information is stored in private fields in the class. If you read or set the values of these fields, you need to access them through some corresponding methods. What do you think are the names of these methods? The properties of a JavaBean are determined by its setter and getter methods, not by its member variables. If the method name is setid, it means setting ID in Chinese. As for which variable you save it, do you use tube? If the method name is getid, it means to get ID in Chinese. As for which variable you get from, do you use tube? Remove the set prefix, and the remaining part is the attribute name. If the second letter of the remaining part is lowercase, change the first letter of the remaining part to small.

For example:

Property name of setid() -- > ID

Attribute name of islast() -- > last

What is the property name of setcpu? -- > cpu

What is the property name of getups? -- > UPS

In short, when a class is used as a JavaBean, the properties of the JavaBean are inferred from the method name. It can't see the member variables inside the Java class at all.

A class that conforms to the characteristics of JavaBeans can be used as an ordinary class, but when it is used as a JavaBean, it must bring some additional benefits before we can understand and apply JavaBeans! The benefits are as follows:

JavaBeans are often used in Java EE development. Many environments require to operate in the way of JavaBeans, which are used and required by others, so you have no choice!

JDK provides some APIs for operating JavaBeans, which are called introspection. If you want to access private X through the geTx method, how to do it is difficult? Using the introspection API to operate JavaBeans is more convenient than using ordinary classes.

Simple introspection of JavaBeans

It mainly uses Java beans. The PropertyDescriptor class is used to get a JavaBean attribute of a Class object attribute set, and then getReadMethod (), getWriteMethod () method is used to get the corresponding get and set methods.

Code example:

Domain class:

[cpp]viewplaincopy

intmain()

Simple introspection:

Complex introspection of JavaBeans

Find and set the X property of a refectpoint object by traversing all the properties of beaninfo. When a class is viewed as a JavaBean in the program, it is to call introspector Getbeaninfo method, the obtained beaninfo object encapsulates the result information of viewing this class as a JavaBean.

Complex introspection:

Using the BeanUtils toolkit to manipulate JavaBeans

Based on the previous introspective example, use the BeanUtils class to get the originally set property first, and then set it to a new value. The result returned when the get property is a string. When the set property is used, any type of object can be accepted, usually a string.

Use the propertyutils class to get the originally set property first, and then set it to a new value. The result returned when getting the property is the original type of the property. When setting the property, only the original type of the property is accepted.

Note: before using these two classes, you need to import commons bean utils. Exe in the Lib folder of the eclipse project jar、commons-logging-1.1. Jar two jar packages, and addtobuildpath.

Code example:

summary

The above is all about Java introspection instance parsing in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>