Common jar packages for commons-beanutils

Source: Internet
Author: User

The Jakarta Commons project provides quite a variety of APIs. The Commons Lang we learned earlier is only a small part of the core of many APIs. There are a considerable number of sub-projects under Commons to solve various practical problems in different directions. BeanUtils is one of them and is used to process JavaBeans. It uses the reflection mechanism of Java to dynamically generate the call code for getter and setter of bean, simulate the creation of a dynamic bean, and so on. This package seems simple, but it is the cornerstone of many open-source projects: for example, in the famous Struts and Spring Framework, we can find BeanUtils shadows. You can guess which celebrity is one of the authors of BeanUtils? That's right, Craig McClanahan, the founder of Struts.

 

The Core Benefit of BeanUtils is that when coding, we do not need to know what type and attributes of the JavaBeans we are processing. Such information can be dynamically obtained, even we don't have to worry about whether such a specific JavaBean class exists. We only need to know that there is a JavaBean instance, from which we need to obtain a property, set the value of a property, or just need an Attribute Table. To do this, it seems that the JavaBean specification provided by Sun cannot find a very direct method. Unless it is hard-coded, getXxxx () and setXxxx () are directly written into our program. However, this greatly increases Code complexity, coupling, and maintenance costs. Fortunately, Commons BeanUtils provides an elegant solution to this problem.

 

We have two ways to get the binary of Commons BeanUtils:

1-find the corresponding jar file from Struts, Spring, or any release package of open-source products dependent on BeanUtils;

2-download from http://www.apache.org/dist/jakarta/commons/beanutils/binaries.

 

Source code of Commons BeanUtils:

Http://www.apache.org/dist/jakarta/commons/beanutils/source/

 

Commons BeanUtils includes the following five packages:

 

Org. apache. commons. beanutils-core package defines a set of Utils classes and interface specifications to be used

Org. apache. commons. beanutils. converters-converts String to a class of the desired type and implements the Converter interface.

Org. apache. commons. beanutils. locale-beanutils locale sensitive version

The locale sensitive version of org. apache. commons. beanutils. locale. converters-converters

Collection class used by org. apache. commons. collections-beanutils

 

Among them, we need to pay special attention to this org. apache. commons. beanutils package. Other packages are auxiliary. Next, let's take a look at how to use the beanutils package.

 

1. propertyutils


It supports three types of attribute values: Simple, Indexed, and Mapped.
Simple attribute access method
PropertyUtils. getSimpleProperty (Object bean, String name)
PropertyUtils. setSimpleProperty (Object bean, String name, Object value)

Implementation Code:
Employee employee = ...;
String firstName = (String)
PropertyUtils. getSimpleProperty (employee, "firstName ");
String lastName = (String)
PropertyUtils. getSimpleProperty (employee, "lastName ");
... Manipulate the values...
PropertyUtils. setSimpleProperty (employee, "firstName", firstName );
PropertyUtils. setSimpleProperty (employee, "lastName", lastName );

Indexed attribute access method
PropertyUtils. getIndexedProperty (Object bean, String name)
PropertyUtils. getIndexedProperty (Object bean, String name, int index)
PropertyUtils. setIndexedProperty (Object bean, String name, Object value)
PropertyUtils. setIndexedProperty (Object bean, String name, int index, Object value)

Implementation Code:
Employee employee = ...;
Int index = ...;
String name = "subordinate [" + index + "]";
Employee subordinate = (Employee)
PropertyUtils. getIndexedProperty (employee, name); // obtain the property based on the value
Employee employee = ...;
Int index = ...;
Employee subordinate = (Employee)
PropertyUtils. getIndexedProperty (employee, "subordinate", index); // according to the index
Attribute Value

Mapped attribute access method
PropertyUtils. getMappedProperty (Object bean, String name)
PropertyUtils. getMappedProperty (Object bean, String name, String key)
PropertyUtils. setMappedProperty (Object bean, String name, Object value)
PropertyUtils. setMappedProperty (Object bean, String name, String key, Object value)
Implementation Code:
Employee employee = ...;
Address address = ...;
PropertyUtils. setMappedProperty (employee, "address (home)", address); // according to the array
// Corresponding attribute values of the Internal Values
Employee employee = ...;
Address address = ...;
PropertyUtils. setMappedProperty (employee, "address", "home", address );

Nested attribute access method // Nested means that the parameter contains components
PropertyUtils. getNestedProperty (Object bean, String name)
PropertyUtils. setNestedProperty (Object bean, String name, Object value)
Implementation Code:
String city = (String) PropertyUtils. getNestedProperty (employee,
"Address (home). city ");

2. beanutils. dynabean and beanutils. dynaclass interface Introduction
DynaBean must have a bean to implement this interface. DynaClass must have a bean attribute set.

BasicDynaBean and BasicDynaClass-basic Dynamic type
Basic APIs:
BasicDynaClass (java. lang. String name, java. lang. Class dynaBeanClass, DynaProperty [] properties)
BasicDynaBean (DynaClass dynaClass)
Let's define the basic code:
// Define a dynamic property set
Dynaproperty [] props = new dynaproperty [] {
New dynaproperty ("Address", java. util. Map. Class ),
New dynaproperty ("subordinate", mypackage. Employee []. Class ),
New dynaproperty ("firstname", String. Class ),
New dynaproperty ("lastname", String. Class)
};
// Create a dynamic class to set the dynamic attribute value
Basicdynaclass dynaclass = new basicdynaclass ("employee", null, props );
Dynabean Employee = dynaclass. newinstance ();
Employee. Set ("Address", new hashmap ());
Employee. Set ("subordinate", new mypackage. Employee [0]);
Employee. Set ("firstname", "Fred ");
Employee. Set ("lastname", "Flintstone ");

Resultsetdynaclass (wraps resultset in dynabeans)-use the resultset
Dynamic JavaBean
API:
ResultSetDynaClass (java. SQL. ResultSet resultSet)
ResultSetDynaClass (java. SQL. ResultSet resultSet, boolean lowerCase)
Connection conn = ...;
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery
("Select account_id, name from MERs ");
Iterator rows = (new ResultSetDynaClass (rs). iterator ();
While (rows. hasNext ()){
// Use dynamic bean for output
DynaBean row = (DynaBean) rows. next ();
System. out. println ("Account number is" +
Row. get ("account_id") +
"And name is" + row. get ("name "));
}
Rs. close ();
Stmt. close ();

RowSetDynaClass (Disconnected ResultSet as DynaBeans)-use RowSet's Dynamic
JavaBean
API:
RowSetDynaClass (java. SQL. ResultSet resultSet)
RowSetDynaClass (java. SQL. ResultSet resultSet, boolean lowerCase)
Connection conn =...; // gets a Connection from the buffer pool
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("SELECT ...");
RowSetDynaClass rsdc = new RowSetDynaClass (rs );
Rs. close ();
Stmt. close ();
...; // Closes the connection and returns the buffer pool.
List rows = rsdc. getRows ();
...; // The row to be processed

3. beanutils. convertutils Introduction
This package is mainly used to convert the functions passed from the request.
Main functions:
ConvertUtils (). convert (java. lang. Object value)
ConvertUtils (). convert (java. lang. String [] values, java. lang. Class clazz)
ConvertUtils (). convert (java. lang. String value, java. lang. Class clazz)
Implementation example:
HttpServletRequest request = ...;
Mybean bean = ...;
Hashmap map = new hashmap ();
Enumeration names = request. getparameternames ();
While (names. hasmoreelements ()){
String name = (string) names. nextelement ();
Map. Put (name, request. getparametervalues (name ));
}
Beanutils. populate (bean, MAP); // use convertutils for conversion
Currently supported conversion types:
Sjava. Lang. bigdecimal
Java. Lang. biginteger
Boolean and Java. Lang. Boolean
Byte and Java. Lang. byte
Char and Java. Lang. Character
Java. Lang. Class
Double and Java. Lang. Double
Float and Java. Lang. Float
INT and Java. Lang. Integer
Long and Java. Lang. Long
Short and Java. Lang. Short
Java. Lang. String
Java. SQL. Date
Java. SQL. Time
Java. SQL. Timestamp

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.