Always use the Common BeanUtils

xiaoxiao2021-03-06  39

Beanutils uses magic reflex technology to achieve a lot of exaggerated features, all C / C era did not want to think. No matter who's project, it will use it on a day. I thought it was a later known, and when I saw it first, I actually missed it.

1. Dynamic Getter, Setter

In this frame of flying, you can't guarantee your getter, setter function, sometimes the property is to be dynamically obtained, just like this:

BeanUtils.getProperty (MyBean, "Code");

The stronger function of Common BeanUtils is that the properties of the embedded object can be directly accessed, as long as the order is separated.

BeanUtils.getProperty (Orderbean, "Address.city");

In contrast, the beanutils of other class libraries is usually very simple, and can not access the embedded object, so sometimes it is necessary to replace them with Commons Beanutils.

BeanUtils also supports properties of List and Map types, such as the following syntax to get the first customer's name in the list of Order customers.

Beanutils.getProperty (Orderbean, "Customers [1] .name");

BEANUTILS uses the ConvertUTILS class to convert the string into the real type of the bean property, easy to extract beans from objects such as HTTPSERVLETREQUEST, or output the bean to the page.

PropertyUtils will reserve the original type of Bean.

2. Beancompartor Dynamic Sort

Drafter, the dynamic setting bean is sorted in which attributes are sorted, and no need for complex conditions in implementing the COMPARE interface to implement the bean.

List people list lists List heads = ...; // Person object list

Collections.Sort (Peoples, New Beancomparator ("age");

If you want to support multiple properties, such as "Order By Lastname, Firstname"

Arraylist sortfields = new arraylist ();

Sortfields.Add (New BeanComparator ("LastName"));

Sortfields.Add (New BeanComparator ("firstname");

Comparatorchain Multisort = New Comparatorchain (Sortfields);

Collects.Sort (Rows, Multisort);

Where Comparatorchain belongs to the Jakata Commons-Collections package. If the AGE property is not a normal type, the constructor needs to be transferred to a COMPARATOR object to sort the AGE variable. In addition, the COMPAREBLECOMPARATOR of the Beancompartor itself will throw an exception when the property is null, and cannot be set or descended. At this time, I have to use the Commons-Collection package ComparatorUtils.

Comparator mycmp = ComparableComparator.getInstance (); mycmp = ComparatorUtils.nullLowComparator (mycmp); // allow null mycmp = ComparatorUtils.reversedComparator (mycmp); // reverse Comparator cmp = new BeanComparator (sortColumn, mycmp);

3. Converter Binds the string of the REQUEST or RESULTSET to the object's properties to extract values ​​from REQUEST, RESULTSET and other objects to be encompassed into the bean. If you don't have the binding of the MVC framework, the following code is written .

String a = request.getParameter ("a");

Bean.seta (a);

String b = .... bean.setb (b); ......

Write a Binder Automatically bind all properties:

MyBean bean = ...;

Hashmap map = new hashmap ();

ENUMERATION NAMES = Request.getParameterNames ();

While (names.hasmoreElements ())

{

String name = (string) Names.nexTelement ();

Map.put (name, request.getParameterValues);

}

Beanutils.Populate (Bean, Map);

Where beanutils's populate method or getproperty, the setProperty method actually calls Convert to convert. However, Converter only supports some basic types, even the java.util.date type is not supported. And it is relatively stupid, when you encounter an unform, you will throw an exception. For the Date type, I refer to its Sqldate type to implement a Converter, and add a function of setting up the date format. To register this Converter, you need the following statement:

ConvertUtilsBean convertUtils = new ConvertUtilsBean (); DateConverter dateConverter = new DateConverter (); convertUtils.register (dateConverter, Date.class); // due to the registration converter, it can no longer use the static method of the BeanUtils, you must create BeanUtilsBean instance BeanUtilsBean beanUtils = New BeanUtilsbean (ConvertUTILS, New PropertyUtilsbean ()); beanutils.setProperty (bean, name, value);

4 other features

4.1 CONSTRUCTORUTILS, dynamically created objects

Public Static Object Invokeconstructor (Class Klass, Object Arg)

4.2 MethodUtils, Dynamic Call Method

MethodUtils.InvokeMethod (bean, methodname, parameter);

4.3 PropertyUtils, dynamic reading when the property is Collection, MAP: Collection: Provides Index

Beanutils.GetIndexedProperty (OrderBean, "Items", 1); or

Beanutils.GetIndexedProperty (Items [1] "); Map: Provides Key Value

BeanUtils.getmappedProperty (ORDERBEAN, "ITEMS", "111"); // key-value goods_no = 111 or

BeanUtils.getmappedProperty (ORDERBEAN, "ITEMS (111)") 4.4 PropertyUtils, directly get the class type of the attribute

Public Static Class getPropertyType (Object Bean, String Name)

4.5 Dynamic Bean See

Use Dynabean to reduce unnecessary VO and FORMBEAN

转载请注明原文地址:https://www.9cbs.com/read-61283.html

New Post(0)