“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
1. Introduction
It is a property access controller that controls access to bean properties. It has a very important subinterface, BeanWrapper (which Spring uses to create beans, but which is a separate story). BeanWrapperImpl is used in the following methods for the convenience of demonstration. In other implementation classes, the execution results may be different, so it is more important to use it.
The main implementation classes are as follows:
2. The attribute
- String NESTED_PROPERTY_SEPARATOR = “.” : Path separator for nested properties. Follow normal Java conventions: for example, “foo.bar” is getFoo().getbar ().
- Char NESTED_PROPERTY_SEPARATOR_CHAR = ‘.
- String PROPERTY_KEY_PREFIX = “[” : indicates the mark at the beginning of the property key, such as index persion. Friends [0].
- Char PROPERTY_KEY_PREFIX_CHAR = ‘[‘ : same as above
- String PROPERTY_KEY_SUFFIX = “]” : indicates the end of an attribute key, such as an index.
- Char PROPERTY_KEY_SUFFIX_CHAR = ‘]’ : Same as above
Method 3.
3.1 Boolean isReadableProperty (String propertyName)
- Function: Determines whether the specified attribute is readable. Return false if the property does not exist
- Demo classes:
- Demo Results:
- Extension? What is readable, and what is the result of privatizing the GET method
- Change the getName() method to private, and execute the code just described to see the result
- Does it change if I put main in Student?
- Readability summary, only if the property’s GET method modifier is public (even if the property is public, it is not readable)
3.2 Boolean isWritableProperty (String propertyName)
-
Function: Checks whether a property is writable. Return false if the property does not exist.
-
Demo classes:
-
Demo Results:
-
Writability: that is, the property’s set method modifier is public
3.3 Class <? > getPropertyType(String propertyName) throws BeansException;
- Effect: Determines the type of the specified attribute, or returns null if the attribute does not exist
- Demo class: same as above
- Demo Results:
3.4 the TypeDescriptor getPropertyTypeDescriptor (String propertyName) throws BeansException;
- Function: Returns the type descriptor for the specified attribute, or null if the attribute does not exist
- TypeDescriptor type: contains the following data
- Demo Results:
3.5 Object getPropertyValue(String propertyName) throws BeansException;
- Function: Returns the value of the specified property if the property’s getter method is not
public
Or if the attribute does not exist, throw an exception directly. - Demo classes:
- Demo Results:
- Summary: Before calling this method, you can call isReadableProperty () to prevent exceptions from being thrown.
3.6 void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;
- Function: The function of this method is to assign a value to the specified property if the setter method modifier for that property is not
public
Throws an exception - Demo classes:
- Demo Results:
3.7 setPropertyValue ()
The function is the same as the 3.6 method, but the value is set in a different way.