Jakarta-Common-on experience BeanUtils
Abstract: Jakarta-Common-on experience BeanUtils
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> I. OVERVIEW
First saw BeanUtils package, in Struts projects, Struts as a tool to use, more functional, Lane estimated the stronger, moved to the Common projects on the bar.
<table Width="676" border="0"> <tr> <td width="397"> BeanUtils a total of four package:
Org.apache.commons.beanutils
Org.apache.commons.beanutils.converters
Org.apache.commons.beanutils.locale
Org.apache.commons.beanutils.locale.converters
After three packages are mainly used for data conversion, centering on a Converter interface There is only one way:
Java.lang.Object convert (java.lang.Class type, java.lang.Object value) ↑ Back Tags: Jakarta
For a value will be converted into another type of type Object. In some automation applications should be used.
Here is not to comment on future interest, or find it useful, and look again. Here to talk about a package.
Second, the Bean test
At the beginning of all the tests before, I wrote a simple Bean to the test, the code below:
Package test.jakarta.commons.beanutils; </ td> <td width="269" align="right" valign="top"> </ td> </ tr> </ table>
(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;
)
Public String getName () (
Return name;
)
Public int getValue () (
Return value;
)
Public void setName (String name) (
This.name = name;
)
Public void setValue (int value) (
This.value = value;
)
Public String toString () (
Return value +"("+ name +")";
)
Public int [] getDays () (
Return days;
)
Public void setDays (int is []) (
= Days is;
)
)
Third, BeanUtils
This is a major application of the Util Bean (Oh, this explanation is no bar), the following are a few examples of ways
/ / Static java.util.Map describe (java.lang.Object bean)
/ / This method returns an Object-readable all the attributes and attribute name / attribute values into a Map, another
/ / Called a class attributes, the attribute value is the Object class name, the class is in fact an attribute java.lang.Object
New Month Month month = (1, "Jan");
Try (
Map map = BeanUtils.describe (month);
Set keySet = map.keySet ();
For (Iterator iter = keySet.iterator (); iter.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 (IllegalAccessException e) (
E.printStackTrace ();
) Catch (InvocationTargetException e) (
E.printStackTrace ();
) Catch (NoSuchMethodException e) (
E.printStackTrace ();
)
Output:
C: \ java> java Month
KeyClass: java.lang.String
ValueClass: java.lang.String
11 days
KeyClass: java.lang.String
ValueClass: java.lang.String
Value 1
KeyClass: java.lang.String
ValueClass: java.lang.String
Class class Month
KeyClass: java.lang.String
ValueClass: java.lang.String
Name Jan
C: \ java>
Map noted that all the key / value are String, regardless of the actual object is the value of the number.
This also corresponds with static void populate (java.lang.Object bean, java.util.Map properties) will be used just to describe the Map to assemble into an object.
Look at this section of code
CAO Xiao Gang may recall that, in order to take an object property uncertain, indeed spent a lot of time,
Little difficulty, but to be 100% correct, and still need to pay a lot of energy.
/ / Static java.lang.String getProperty (java.lang.Object bean, java.lang.String name)
New Month Month month = (1, "Jan");
Try (
System.out.println (BeanUtils.getProperty (month, "value"));
) Catch (Exception e) (
E.printStackTrace ();
)
/ / Output: 1.
And there are similar getProperty getIndexedProperty, getMappedProperty,
To getIndexedProperty example:
New Month Month month = (1, "Jan");
Try (
System.out.println (BeanUtils.getIndexedProperty (month, "days", 1));
System.out.println (BeanUtils.getIndexedProperty (month, "days [1 ]"));
) Catch (Exception e) (
E.printStackTrace ();
)
These two calls are the same.
BeanUtils also in a way:
Static void copyProperties (java.lang.Object dest, java.lang.Object orig)
It is really useful, but also remember that the struts are copyProperties flying in, I even wonder if the entire BeanUtils initially because this method is not only needs to write them.
It will target properties in the orig copied to the dest of work.
4. PropertyUtils
BeanUtils this category and class in many ways are the same parameters, but the return value different.
BeanUtils focus on the "Bean", the return value is usually String, and PropertyUtils focus on attributes, and its return value is usually Object.
5. ConstructorUtils
This category is divided into two main ways: one is by construction method is to create an object.
In fact most of the time by construction method is intended to create object, here only tell us about object creation.
/ / Static java.lang.Object ConstructorUtils.invokeConstructor
/ / (Java.lang.Class klass, java.lang.Object [] args)
/ / According to a java.lang.Class construction method and the corresponding parameters to create an object.
Object obj = ConstructorUtils.invokeConstructor (Month.class, (new Integer (1), "Jan"));
Month month = (Month) obj;
Try (
System.out.println (BeanUtils.getProperty (month, "value"));
) Catch (Exception e) (
E.printStackTrace ();
)
Output that construction method call is a success.
If you need mandatory designated constructor parameter types, can call:
Object args [] = (new Integer (1), "Jan");
Class [] = (int.class argsType, String.class);
Object obj;
Obj = ConstructorUtils.invokeExactConstructor (Month.class, args, argsType);
Month month = (Month) obj;
System.out.println (BeanUtils.getProperty (month, "value"));
ArgsType designated parameter type.
6. ConstructorUtils Create Object there is a residual method: invokeExactConstructor the method more stringent requirements on the parameters, transfer into must be in strict conformity with the parameters of the construction method of the list of parameters.
For example:
Object args [] = (new Integer (1), "Jan");
Class [] = (int.class argsType, String.class);
Object obj;
/ / Below this call will not succeed because args [0] types Integer, and not int
/ / Obj = ConstructorUtils.invokeExactConstructor (Month.class, args);
/ / This one can, because argsType designated types.
Obj = ConstructorUtils.invokeExactConstructor (Month.class, args, argsType);
Month month = (Month) obj;
System.out.println (BeanUtils.getProperty (month, "value"));
7. MethodUtils
And ConstructorUtils similar, but invoked, usually need to specify the parameters of a method name.
8. DynaClass / DynaBean
BeanUtils This seems to be the most interesting part of a very simple, simple to look at the interface of these two methods do not understand why these two interface to design. But after that ResultSetDynaClass, understand. Below are the java doc in the code:
ResultSet rs = …;
ResultSetDynaClass rsdc = new ResultSetDynaClass (rs);
Iterator rows = rsdc.iterator ();
While (rows.hasNext ()) (
DynaBean row = (DynaBean) rows.next ();
… Process this row …
)
Rs.close ();
This is a ResultSet original packaging, ResultSetDynaClass realized DynaClass its iterator method returns an ResultSetIterator, it is realized DynaBean interface.
Will receive a DynaBean, we can use
DynaBean row = (DynaBean) rows.next ();
System.out.println (row.get ( "field1")); / / field1 is one of the name of a field
Another category RowSetDynaClass look at the use, under the code n:
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc: mysql: / / localhost/2hu? UseUnicode = = true & characterEncoding GBK";
String username = "root";
String password = "";
Java.sql.Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Try (
Class.forName (driver). NewInstance ();
Con = DriverManager.getConnection (url);
Ps = con.prepareStatement ( "select * from forumlist");
Rs = ps.executeQuery ();
/ / Print to first for the results of testing behind.
While (rs.next ()) (
System.out.println (rs.getString ( "name"));
)
Rs.beforeFirst ();// beforeFirst here must be used, because the only RowSetDynaClass rolling forward from the current position
RowSetDynaClass rsdc = new RowSetDynaClass (rs);
Rs.close ();
Ps.close ();
List rows = rsdc.getRows ();// return to a standard List, storage is DynaBean
For (int i = 0; i
System.out.println (b.get ( "name"));
)
) Catch (Exception e) (
E.printStackTrace ();
)
Finally (
Try (
Con.close ();
) Catch (Exception e) (
)
)
Is not very interesting? Packaging, a ResultSet data, the price of memory occupied. If a table with 100,000 records, rsdc.getRows () will return 100,000 records. @ @
It must be noted that ResultSetDynaClass RowSetDynaClass and the difference between:
1, is based on the Iterator ResultSetDynaClass, only a return to a record, and is based on RowSetDynaClass List, a one-time return of all the records. Direct impact on the comparison of data for a long time ResultSetDynaClass would be more rapid, and ResultSet RowSetDynaClass need to have all the data in read out (and stored in its internal), will take up too much memory, and speed will be slower.
2, ResultSetDynaClass a deal only with a record, in the complete processing before ResultSet not closed.
3, ResultSetIterator the next () method returns the DynaBean is at a fixed its internal
Object, in each next (), the internal value will be changed. Their purpose in doing so is saving memory, if you need to preserve every
DynaBean, generated on the need to create another DynaBean, and data replication past, java doc Below is the code:
ArrayList results = new ArrayList (); / / To hold copied list
ResultSetDynaClass rsdc = …;
DynaProperty properties [] = rsdc.getDynaProperties ();
BasicDynaClass bdc =
New BasicDynaClass ( "foo", BasicDynaBean.class, rsdc.getDynaProperties ());
Iterator rows = rsdc.iterator ();
While (rows.hasNext ()) (
DynaBean oldRow = (DynaBean) rows.next ();
DynaBean newRow = bdc.newInstance ();
PropertyUtils.copyProperties (newRow, oldRow);
Results.add (newRow);
)
In fact DynaClass / DynaBean can be used in many places, storage of various types of data. Want to own it. Hei hei.
9, since the definition of CustomRowSetDynaClass
RowSetDynaClass wrote two years ago with a goal of the same category, but more than one function, paging, data need only take,
This will reduce memory footprint.
Look at the section of code:
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc: mysql: / / localhost/2hu? UseUnicode = = true & characterEncoding GBK";
String username = "root";
String password = "";
Java.sql.Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Try (
Class.forName (driver). NewInstance ();
Con = DriverManager.getConnection (url);
Ps = con.prepareStatement ( "select * from forumlist order by name");
Rs = ps.executeQuery ();
/ *
While (rs.next ()) (
System.out.println (rs.getString ( "name"));
)
Rs.beforeFirst ();
* /
/ / The second parameter that the first few pages, a third of the size of that page
CustomRowSetDynaClass rsdc = new CustomRowSetDynaClass (rs, 2, 5);
/ / RowSetDynaClass rsdc = new RowSetDynaClass (rs);
Rs.close ();
Ps.close ();
List rows = rsdc.getRows ();
For (int i = 0; i
System.out.println (b.get ( "name"));
)
) Catch (Exception e) (
E.printStackTrace ();
)
Finally (
Try (
Con.close ();
) Catch (Exception e) (
)
)
Here used a CustomRowSetDynaClass category, construction method has been added to page and pageSize two parameters, so that regardless of the number of database records, most of the records from pageSize, if pageSize ==- 1, the same functions and RowSetDynaClass. </ Td> </ tr> <tr>
Releated Java Articles
Comments
Leave a Reply






