BeanWrapper is a Spring tool for manipulating javaBean properties, allowing you to modify the properties of an object directly.

The following utility classes are well known for manipulating bean properties:

  • Apache’s BeanUtils and PropertyUtils
  • 2. BeanMap and BeanCopier of Cglib
  • 3. The spring BeanUtils

The main features of BeanWrapper in Spring are:

  • 1. Supports setting nested properties
  • 2. Support type conversion of attribute values (set ConversionService)
  • 3. Provides analysis and operation of standard Javabeans: the ability to get and set property values (individually or in batches), get property descriptors, and query property readability/writability.

BeanWrapper itself is an interface that provides a whole set of methods for processing beans. The source code is as follows:

public interface BeanWrapper extends ConfigurablePropertyAccessor {
	 // Specify a limit for automatic array and collection growth. This is infinite by default on normal BeanWrapper.
	void setAutoGrowCollectionLimit(int autoGrowCollectionLimit);
	// Returns limits on automatic growth of arrays and collections.
	int getAutoGrowCollectionLimit(a);
    // Returns the bean instance wrapped by this object, if any
	Object getWrappedInstance(a);
	// Returns the type of the wrapped JavaBean object.Class<? > getWrappedClass();// Get the PropertyDescriptors of the wrapped object (as determined by standard JavaBeans introspection).
	PropertyDescriptor[] getPropertyDescriptors();
	// Gets the property descriptor for the specific property of the wrapper object.
	PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;
}
Copy the code

The above BeanWrapper is based on version 4.3.6, and this interface has changed slightly since version 4.1. BeanWrapperImpl is BeanWrapper implementation class, the parent class is AbstractNestablePropertyAccessor BeanWrapperImpl, through the BeanWrapper are capable of handling properties.

Here is an example of wrapping an object with a BeanWrapper:

package com.glmapper.spring.test;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.PropertyValue;
/** * BeanWrapper test class */
public class BeanWrapperTest {
    public static void main(String[] args) {
        User user=new User();
        // Wrap the User object into a BeanWrapper using PropertyAccessorFactory
        BeanWrapper bw=PropertyAccessorFactory.forBeanPropertyAccess(user);
        // Method 1: Directly set the attribute value
        bw.setPropertyValue("userName"."Zhang");
        // Method 2: Set the value using PropertyValue
        PropertyValue pv=new PropertyValue("userName"."Bill"); bw.setPropertyValue(pv); System.out.println(user.getUserName()); }}// a User class
class User{
    private String userName;
    public String getUserName(a) {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName; }}Copy the code

In Spring, many Bean properties are handled through BeanWrapper, such as the common HttpServletBean property Settings.

Note: This digest of my blog garden articles, some packaging, in the Spring source series. In the Spring BeanWrapper

Welcome to follow the wechat official account