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

1. Introduction

PropertyEditorRegistry: as the name suggests, the main function is to save the PropertyEditor (PropertyEditor), as required to return the corresponding PropertyEditor.

PropertyEditor: interface to the PropertyEditor, which specifies the transformation interface methods for converting external setting values to internal JavaBean property values

This article mainly talks about their two functions and relations

2.PropertyEditor

2.1 Inheritance Relationship

From the above figure, we can find its main commonly used implementation classes, which are basically below PropertyEditorSupport. We can usually inherit it to implement our own custom property editor, such as the common: String type property editor, Character type editor…. And so on, all subclasses of it.

2.2 Main Methods

  • void setValue(Object value); : sets the value of the changed object, which must be converted to a wrapper class if it is a primitive type, for example, int to be converted to Integer
  • Object getValue() : Gets the value of the property. The primitive data must be converted to the wrapper type
  • Boolean isPaintable() : Determines whether this property editor is drawable.
  • Void paintValue(java.awt.graphics GFX, java.awt.Rectangle Box) : Draws a representation of a value to a given area of the screen.
  • String getJavaInitializationString () : returns can be used to set the attribute of the Java code fragments matching to edit the current state.
  • String getAsText() : Gets the value of this property as text, returning a String result
  • Void setAsText (String text) throws Java. Lang. IllegalArgumentException: by parsing the given String to set the attribute values, if can’t parse will throw exceptions.
  • String[] getTags() : Returns an array of strings representing valid attribute values (such as true and false for Boolean attributes) so that the property editor can display them in a dropdown. By default, null is returned, indicating that the property has no matching finite set of character values
  • Java.awt.Com ponent getCustomEditor() : Returns a component that lets you directly edit the current property value. If empty, it may be empty
  • Boolean supportsCustomEditor() : Determines whether this property supports custom editing.
  • Void addPropertyChangeListener (PropertyChangeListener listener) : add a listener to value changes, the duty after modification, trigger event listeners
  • Void removePropertyChangeListener (PropertyChangeListener listener) : remove the above set of listeners

3.PropertyEditorRegistry

3.1 Main Inheritance relationship

This diagram will be used later in our discussion of Spring container initialization. As you can see, there are several important implementation classes: BeanWrapperImpl, TypeConverterSupport, etc.The following diagram mainly lists inheritance relationships related to data binding

3.2 Main Methods

  • void registerCustomEditor(Class<? > requiredType, PropertyEditor propertyEditor);
    • Registers a property editor of the specified type
    • RequiredType: Specifies the property type
    • PropertyEditor: propertyEditor
  • void registerCustomEditor(@Nullable Class<? > requiredType, @Nullable String propertyPath, PropertyEditor propertyEditor);
    • Registers a custom property editor for the specified type
    • RequiredType: Specifies the property type
    • PropertyPath: Path (name or nested path) to the property if null registers the editor for all properties of the given type
    • PropertyEditor: propertyEditor
  • PropertyEditor findCustomEditor(@Nullable Class<? > requiredType, @Nullable String propertyPath)
    • Finds the custom property editor by the specified type and property
    • RequiredType: Specifies the type
    • PropertyPath: Path to the specified property

4. To summarize

It is easy to see from the above method that the PropertyEditorRegistry is mainly used to add and find the PropertyEditor. In Spring, the bean is created by fetching the corresponding PropertyEditor through the PropertyEditorRegistry and setting or modifying its value for the corresponding property.