Analysis of copyproperties,populate implementation of Beanutils and its easy to be induced bug__javaee

Source: Internet
Author: User

The beanutils copyproperties method is used to copy the value of the member variable in the orig to dest, which changes the existing dest to a copy of the orig.

public void Copyproperties (object dest, Object orig) {
.......
}

The Beanutils populate method is used to convert the value (string or string[) in map<key,value> to the property of the target bean, and the Key in the map is the property name of the target bean.

public static void populate (Object bean, Map properties) {
......
}

Copyproperties also supports the functionality in populate (in Apache Javadoc, which explicitly indicates that this method is specifically defined and used to parse HTTP request parameters. It is not recommended for normal use. They recommend using the Beanutils.copyproperties method.

The implementation of Beanutils.copyproperties and populate is to set the result of the transformation to the corresponding property of the target bean, with the help of the converter (Converter), with each element in the source bean (or map). For example, in HTTP applications where the data needs to be extracted from HTTP request, HTTP request passes all the variables of string or string array type and the target type may be various, for example, HTTP request has a name= Visitdate,value= ' 2009-05-13′ parameter, and the target bean's Visitdate property type is java.util.Date.
The Beanutils copyproperties and populate need to convert the data types between the source and target objects in conjunction with the converter (converter). Described in Beanutils.copyproperties Javadoc (Copy property values from the origin beans to the destination beans for all cases whe Re the property names are the same--can be copied from the source bean to the target bean as long as the attribute name is the same, provided that the function is to be implemented through the converter. Converter is a very important concept in the use of Beanutils's copyproperties and populate, which provides a powerful extension capability.

/****************************************************************/
Public interface Converter {
public object Convert (Class type, Object value);
}
/****************************************************************/

The parameter type of the Convert method is the type of the target conversion, the parameter value is the converted value, and the return value is the result of the conversion. Converter can be implemented by registering a custom converter when there is a need to customize or extend it. For example, the Beanutil DateConverter does not support the conversion of string to java.util.Date, and support can be achieved by extending dateconverter. Of particular note is the converter is registered at the ClassLoader level, that is, in a class Only one converter can work at the same time in loader (beanutils copyproperties and populate decide to enable the converter based on the type of the target bean attribute). In my previous project there was a type of converter that would have been registered at a particular time in the other modules, and the newly registered converter did not support the transformations I needed, resulting in the presence of NPE in my module. So when using Beanutils.populate and copyproperties, pay attention to the following points:

1. Register a converter only when the system is initialized, not during the execution of a feature.
2. Converter to be able to support the use of various modules of the project requirements
3. Careful use or think about Beanutils.populate and copyproperties is the way you want to do it.

In the module where I use Beanutils.populate, the intention is to implement a property copy of the bean and map without converting the type and value. As for why the use of beanutils.populate is entirely misguided in its support for map, Propertyutilsbean.copyproperties and Beanutils.copyproperties also support map. In terms of the functionality I want to implement, it's better to be satisfied with the Propertyutilsbean.copyproperties method. The Propertyutilsbean.copyproperties method does not have the logic of type conversion, so it is necessary for the programmer to ensure compatibility between the target and the source bean properties, and because the execution of this propertyutilsbean.copyproperties is more efficient.

reference materials

Usage and difference of beanutils.copyproperties and propertyutils.copyproperties l

Http://commons.apache.org/beanutils/v1.8.0/apidocs/org/apache/commons/beanutils/PropertyUtilsBean.html

Http://commons.apache.org/beanutils/v1.8.0/apidocs/org/apache/commons/beanutils/BeanUtils.html

First Use Apache.commons.beanutils.BeanUtils

Transferred from HTTP://WWW.DANIEL-JOURNEY.COM/ARCHIVES/72

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.