In Spring configuration files, we often set various types of property values of Bean through literal values, and this function is implemented through the property editor.
Any class that implements the java.beans.PropertyEditor interface is a property editor. It can convert the external value to be set to the corresponding type inside the JVM, so the property editor is actually a type converter.
1 JavaBean Editor
The JavaBean editor developed by Sun is largely prepared for the IDE. It allows the IDE to set the properties of JavaBeans visually.
Java defines the method of setting JavaBean properties through java.beans.PropertyEditor. Through BeanInfo, it describes which properties of JavaBean are customizable. In addition, it also describes the correspondence between customizable properties and PropertyEditor.
The correspondence between BeanInfo and JavaBean is established through the naming of the norm between the two. The BeanInfo corresponding to JavaBean adopts the following naming specification: <Bean>BeanInfo. For example, the BeanInfo corresponding to BookBean is BookBeanBeanInfo. When a JavaBean is registered with the IDE together with its attribute editor, when the JavaBean is customized in the development interface, the IDE will find the corresponding BeanInfo according to the JavaBean specification, and then find the JavaBean attribute description (which attribute editor is used, etc.) based on the description information in the BeanInfo, and then generate a specific development editing interface for the JavaBean.
Java provides a manager for managing default property editors: PropertyEditorManager, which holds some common types of property editors. If a JavaBean's common type property does not explicitly specify its property editor through BeanInfo, the IDE will automatically use the default editor for the corresponding properties registered in PropertyEditorManager.
1.1 PropertyEditor
PropertyEditor is the property editor interface that defines an interface method that converts external set values to internal JavaBean property values. The main interface methods are explained as follows:
| method | illustrate |
|---|---|
| Object getValue() | Returns the current value of the attribute, the basic type is encapsulated into the corresponding wrapper type |
| void setValue(Object newValue) | Set the value of the attribute, the basic type is passed in as the wrapper type |
| String getAsText() | Use strings to represent attribute objects so that external attribute editors can display them visually. Returns null by default, indicating that the property cannot be represented as a string. |
| void setAsText(String text) | Use a string to update the internal value of the property, which is generally passed from the external property editor. |
| String[] getTags() | Returns an array of strings representing valid attribute values so that the attribute editor can display it in the following way. Returns null by default. |
| String getJavaInitializationString() | Provides an initial value for the property, which the property editor uses as the default value for the property. |
The PropertyEditor interface is a communication bridge between internal property values and external setting values.
Java provides a convenient implementation class for PropertyEditor: PropertyEditorSupport, which implements the PropertyEditor interface. We can design our own property editor by extending this class.
1.2 Bean Attribute Description (BeanInfo)
BeanInfo describes editable properties in JavaBean and the corresponding property editor, each property corresponds to a property descriptor PropertyDescriptor.
The constructor of PropertyDescriptor has two parameters: PropertyDescriptor(String propertyName, Class beanClass), where propertyName is the property name; beanClass is the Class corresponding to JavaBean.
PropertyDescriptor also has a setPropertyEditorClass(Class propertyEditorClass) method, which specifies an editor for JavaBean properties.
The most important method in the BeanInfo interface is: PropertyDescriptor[] getPropertyDescriptors() , which will return the JavaBean property descriptor array.
A commonly used implementation class for the BeanInfo interface is SimpleBeanInfo, which we can implement customized functions by extending this class.
2 Spring default property editor
Spring's property editor is different from the traditional property editor for IDE development. It does not have a UI interface and just converts the text configuration value in the configuration file to the corresponding value of the Bean property.
Spring provides a default property editor for common property types in PropertyEditorRegistrySupport, which is divided into 3 major categories, with a total of 32:
| type | illustrate |
|---|---|
| Basic data types | [1] Basic data types, such as: boolean, int, etc.; [2] Basic data type encapsulation classes, such as: Boolean, Integer, etc.; [3] Basic data type arrays: char[] and byte[]; [4] Large numbers: BigDecimal and BigInteger. |
| Collection Class | Collection, Set, SortedSet, List, and SortedMap. |
| Resources | Class, Class[], File, InputStream, Locale, Properties, Resource[], and URL. |
There are two Map type variables in PropertyEditorRegistrySupport that save property editors:
| Variable name | illustrate |
|---|---|
| defaultEditors | The editor that saves the default attribute type, the key of the element is the attribute type, and the value is the corresponding attribute editor instance. |
| customEditors | Save user-defined attribute editors, the key values of elements are the same as defaultEditors. |
3 Custom Spring Properties Editor
If our application defines a special type of attribute and wants to configure the attribute value literally in the configuration file, then we can write a custom attribute editor and register it with the Spring container to implement it.
Most of the Spring default property editors extend from java.beans.PropertyEditorSupport. We can customize the property editor by extending PropertyEditorSupport. In Spring environment, you only need to convert the literal value in the configuration file to an object of property type, and you do not need to provide a UI interface, so you only need to override the setAsText() method of PropertyEditorSupport (∩_∩)O Haha~.
Suppose we have two entity Book and Author, and we hope that when configuring Book, we can directly set the Author name.
Book.java
public class Book { /** * Author*/ private Author author; /** * Book title*/ private String name; //Omit the get/setter method}Author.java
public class Author { private String name; //omit the get/setter method}First, customize the property editor of the author:
public class CustomPropertyEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if(text==null||text.length()==0){ throw new IllegalArgumentException("Format Error"); } Author author=new Author(); author.setName(text); //Call the method of the parent class to setValue(author); }} If you use BeanFactory, you need to manually call registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) method to register the custom property editor; if you are using ApplicationContext, you only need to register the CustomEditorConfigurer in the configuration file. CustomEditorConfigurer implements the BeanFactoryPostProcessor interface, so it is a factory postprocessor for beans.
Now register a custom property editor:
<!-- Register a custom property editor --><bean> <property name="customEditors"> <map> <!-- key: property type; value: property editor --> <entry key="net.deniro.spring4.editor.Author" value="net.deniro.spring4.editor.CustomPropertyEditor" /> </map> </property></bean><bean id="book"> <property name="name" value="Kafka by the sea"/> <!-- Inject this property using the editor defined previously --> <property name="author" value="Haruki Murakami"/></bean>
When setting the author property of the book, the custom property editor registry will be retrieved. When the property editor CustomPropertyEditor corresponding to the author property type is found, it will convert "Haruki Murakami" into an Author object.
According to the specification, Java will look for whether <JavaBean>Editor 's class exists under the same class package of JavaBean; if it exists, it will automatically use <JavaBean>Editor as the property editor of the JavaBean. Spring also supports this specification.
So if there is a property editor class named AuthorEditor under the class package, then there is no need to register a custom property editor in the configuration file O(∩_∩)O Haha~
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.