A small summary of the Java introspection mechanism
The reflection mechanism in Java is to get the method of class and the component of object by name, which is applicable to all Java classes, but sometimes it is more troublesome to use it. JavaBean is a special Java class that adheres to the JavaBean specification that all members are private members, and that each member has a publicly read and set method (getter and setter) that complies with the named specification. Because JavaBean has these characteristics, Sun has launched a special access to JavaBean members of the technology to facilitate their access, that is, introspective technology.
First of all, introspection uses several classes (interfaces) for Java.beans.propertydescriptor,java.beans.introspector and Java.beans.beanInfo. The PropertyDescriptor is a description of a bean property that provides a series of methods for manipulating the Bean property, Introspector is a tool class that provides a series of methods for obtaining bean information. BeanInfo is a description of a bean that can be used to get information inside the bean.
Here are a few small demos.
The first is to define a bean, see the following code:
Package com.heima.domain;
public class Student {
private String name;
private int age;
private address address;
@Override public
String toString () {return
"Student [name=] + name +", age= "+ Age +", address= "
+ Address + "]";
}
Public Student () {
} public
String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
Public Address getaddress () {return address
;
}
public void setaddress (address) {
this.address = address;
}
Public Student (String name, int age, address address) {
this.name = name;
This.age = age;
this.address = address;
}
The following is an assignment to a property of a bean:
private void SetValue () throws Exception {
Student Student = new Student ();
Get a description of the attribute
PropertyDescriptor descriptor = new PropertyDescriptor ("name", Student.class);
Get Write methods Method
= Descriptor.getwritemethod ();
Use reflection to invoke the method, carry on the assignment
Method.invoke (student, "Dog Daner");
SYSTEM.OUT.PRINTLN (student);
}
Another example of assigning values to all the properties of a bean:
private void CopyProperties2 (HttpServletRequest request) throws Exception {
map<string, string[]> Map = Request.getparametermap ();
Student Student = new Student ();
Gets the parameter Map
for (map.entry<string, string[]> me:map.entrySet ()) {
String name = Me.getkey ();
String[] values = Me.getvalue ();
Each attribute is given a description object
propertydescriptor descriptor = new PropertyDescriptor (name, student.class);
Method method = Descriptor.getwritemethod ();
Here because values may be an array, not strong to a pair of objects will appear exception
if (Values.length > 1) {
Method.invoke (student, (Object) values);
} else {
Method.invoke (student, values);
}
}
}
We can see that using the introspection mechanism to manipulate the bean properties is complicated, and there must be someone to do the tool to help with this series of actions, and finally another example of using the map to assign values to the properties in the bean
private void Copyproperties (HttpServletRequest request) {
Student Student = new Student ();
try {
beanutils.populate (student, Request.getparametermap ());//Use the Beanutils populate method to assign the key value pairs in the map to the Bean
catch (Illegalaccessexception e) {
e.printstacktrace ();
} catch (InvocationTargetException e) {
E.printstacktrace ();
}
Some of the methods used in the Beanutils, beanutils with the package name is Org.apache.commons.beanutils.BeanUtils, is given by Apache, to solve the problem of using introspection mechanism. Use to import two packages: Commons-beanutils-1.9.2.jar and Commons-logging-1.1.1.jar.
The use of introspective mechanism is simpler than reflection, the difference is more obvious after the use of tools, in the web work can be how much use, or their own packaging, with more smoothly ^ ^.