“This is the 20th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Definition 1.

BeanWrapper is a wrapper around beans when Spring creates them. It provides operations for parsing and manipulating standard Javabeans: the ability to get and set property values (individually or in batches), get property descriptors, and query properties for read-write. Spring first creates a BeanWrapper before creating an object, which allows Spring to access the bean’s properties in a uniform way.

1.1 class diagram

  1. PropertAccessor: Property access controller that controls access to bean properties
  2. PropertyEditorRegistry: Registry of bean property editors that holds bean property editors
  3. TypeConverter: TypeConverter that defines the method interface for type conversions.

Through the class diagram above, we should also can know about, what its main role is: to get and set the default implementation class attribute value in the Spring is org. Springframework. Beans. BeanWrapperImpl. We’re going to focus on BeanWrapperImpl.

2. Method introduction

Its main methods are shown below:

The gray method is mainly the method in its parent interface. I have talked about it in the parent interface in front, but I won’t talk about it here. If you don’t understand, you can click the link above to have a look. Next I’ll focus on the method shown in the figure below.

2.1 void setAutoGrowCollectionLimit (int autoGrowCollectionLimit)

Main function: Sets the maximum length limit of an array or collection that can be received. Default implementation: Note: it can be and the following int getAutoGrowCollectionLimit () with demonstration

2.2 int getAutoGrowCollectionLimit ()

Gets the maximum number limit that it sets for receiving arrays or collections

Demo Results:

  1. Default quantity:
  2. Quantity after setting:

2.3 the Object getWrappedInstance ();

Returns the bean instance wrapped by this object. Default implementation: This is where the wrapperObject is set

Demo Results: If this parameter is not set, exceptions will be thrown:

2.4 Class <? > getWrappedClass();

Returns the default implementation of the class object for the bean wrapped by this object:Demo Results:

2.5 PropertyDescriptor [] getPropertyDescriptors ();

Default implementation of wrapper object PropertyDescriptorFirst get beanWeapper CachedIntrospectionResults (get lazy initialization CachedIntrospectionResults instance)PropertyDescriptorCache: Caches property names — propertyDescriptorCacheTake all the PropertyDescriptor and return it as an array.Demo Results:

This contains a lot of information about the property, the property type, the read method, the write method, and so on.

2.6 PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;

Main function: according to the attribute name PropertyDescriptor remove this property, take not to throw exception default implementation: getPropertyAccessorForPropertyPath: Recursive navigation path attribute to return nested accessors is similar to the above, from the above said the cachedIntrospectionResults by name PropertyDescriptor removed itDemo Results:

If the second test attribute is not present, an exception is thrown

3. Summary

This is mainly to pave the way for Spring bean creation, so that’s it for this article.