Author: SonyMusic 2003.05.13
I. Overview
For the first time, I saw the beanutils package, in the Struts project, as a tool for Struts, used,
The more you get the stronger, you move to the CommON project.
Beanutils has four packages:
Org.apache.commons.beanutils
Org.apache.commons.beanutils.converters
Org.apache.commons.beanutils.locale
Org.apache.commons.beanutils.locale.Converters
The latter three packs are mainly used for data conversion, surrounding a CONVERTER interface, only one way:
Java.Lang.object Convert (java.lang.class type, java.lang.object value),
Used to convert a value to another type of object. It should be useful in some automated applications.
It is not commented here, I have been interested, or I feel useful, and then study.
Here is only the first package.
Second, Bean for testing
Before I started all the tests, I wrote a simple bean for tested, the code is as follows:
Package test.jakarta.commons.beanutils;
/ **
* @Author Sonymusic
*
* /
Public class month {
Private int value;
PRIVATE STRING NAME;
Private int [] days = {11, 22, 33, 44, 55};
Public Month (int V, string n) {
Value = v;
Name = n;
}
/ **
* Returns the name.
* @Return String
* /
Public string getname () {
Return Name;
}
/ **
* Returns the value.
* @Return Int
* /
Public Int getValue () {
Return Value;
}
/ **
* Sets the name.
* @Param Name the name to set
* /
Public void setname (String name) {
THIS.NAME = Name;
}
/ **
* Sets the value.
* @Param Value The Value To Set
* /
Public void setValue (int value) {
THIS.VALUE = VALUE;
}
/ **
* @see java.lang.Object # toString ()
* /
Public string toString () {
RETURN VALUE "(" Name ")";
}
Public int [] getDays () {
Return day;
}
Public void setdays (int [] is) {
Days = IS;
}
}
Third, beanutils
This is a main application to bean Util (huh, this explanation is very good), the following is the example of several methods
// static java.util.map describ (java.lang.object bean)
/ / This method returns all readable properties in an object, and puts the property name / property value in a map, and // A property named Class is the name of Object, in fact, Class Is a property of java.lang.object
Month Month = New Month (1, "Jan");
Try {
Map map = beanutils.describe (Month);
Set keyset = map.keyset ();
ITerator it = keyset.iterator (); it.hasnext ();) {
Object element = (object) iter.next ();
System.out.println ("KeyClass:" Element.getClass (). GetName ());
System.out.println ("ValueClass:" map.get (element) .Getclass (). Getname ());
System.out.print (Element "/ T");
System.out.print (map.get (element));
SYSTEM.OUT.PRINTLN ();
}
} catch (IllegaCcessException E) {
E.PrintStackTrace ();
} catch (invocationTargeTexception E) {
E.PrintStackTrace ();
} catch (nosuchmethodexception e) {
E.PrintStackTrace ();
}
The output is:
KeyClass: java.lang.string
ValueClass: java.lang.string
Value 1
KeyClass: java.lang.string
ValueClass: java.lang.string
Class class test.jakarta.commons.beanutils.month
KeyClass: java.lang.string
ValueClass: java.lang.string
Name Jan
Note that key / value in all MAP is String, regardless of the actual value in the object of Object.
There is also static void populate (java.ient.ubject bean, java.util.map property)
Used to put the MAP that DESCRIBE is replaced into an object.
Look at such a code
Cao Xiao Gang may still remember that in order to take an unstertic object, it has a lot of time,
The difficulty is not difficult, but it is necessary to do 100%, still need to pay great effort.
// static java.lang.string getProperty (java.lang.object bean, java.lang.string name)
Month Month = New Month (1, "Jan");
Try {
System.out.println (beanutils.getProperty);
} catch (exception e) {
E.PrintStackTrace ();
}
// Output is: 1
Similar to getproperty and getIndexedProperty, GetMappedProperty,
Take GetIndexedProperty as an example:
Month Month = New Month (1, "Jan"); TRY {
System.out.println (Beanutils.GetIndexedProperty (Month, "Days", 1);
System.out.println (BeanUtils.GetIndexedProperty)));
} catch (exception e) {
E.PrintStackTrace ();
}
These two calls are the same.
There is also a method in BeanUtils:
Static void CopyProperties (java.lang.object dest, java.lang.object orig)
It is really useful, I still remember that Struts is full of CopyProperties, I even doubt the entire beanutils original
Is it because the needs of this method are written.
It copies the properties in the object orig to DEST.
Four, PropertyUtils
Many methods of this class and beanutils classes are the same on the parameters, but the return value is different.
Beanutils focuses on "bean", the return value is usually String, and PropertyUtils focuses on attributes.
Its return value is usually Object.
Five, constructorutils
The method in this class is mainly divided into two, one is to obtain a construction method, one is to create an object.
In fact, most of the time to get a constructor is to create an object, here only introduce the creation of objects.
// static java.lang.Object Constructorutils.InvokeConstructor
//(java.lang.class klass, java.lang.object [] args
// Create an object based on a java.lang.class and the corresponding constructor parameters.
Object Obj = constructorutils.invokeconstructor (Month.class, {new integer (1), "jan"});
Month Month = (MONTH) OBJ;
Try {
System.out.println (beanutils.getProperty);
} catch (exception e) {
E.PrintStackTrace ();
}
The output is proved that the call to the constructor is successful.
If you need to force the parameter type of the constructor, you can call:
Object [] args = {new integer (1), "jan"};
Class [] argstype = {INT.CLASS, STRING.CLASS}
Object obj;
Obj = constructorutils.invokeexactConstructor (Month.class, args, argstype);
Month Month = (MONTH) OBJ;
System.out.println (beanutils.getProperty);
ArgStype specifies the type of parameters.
================================================ to be Continue ...