PropertyResolver property solver, mainly has two functions:
Get the corresponding propertyValue property value (getProperty) through the propertyName property name.
Replace the property placeholder in the format ${propertyName:defaultValue} with the actual value (resolvePlaceholders).
Note: The property values obtained by getProperty are all values after calling resolvePlaceholders for placeholders replacement.
The component system diagram is as follows:
PropertyResolver interface:
This interface defines all the functions a component has. One is to get the corresponding value through the key. When the value cannot be obtained, there are 3 options: return a null value, use the specified default value, or throw an illegal state exception. The obtained value is String type by default. Of course, it can also be considered as specifying a type, which depends on the ConversionService for type conversion.
There is another problem: the property value can contain placeholders in the format of ${}, so another function has been added to the interface is to replace placeholders in the property value (note: placeholders are not allowed for attribute names, and even if they exist, components will not be replaced as placeholders). When the placeholder cannot be replaced, there are two options: keep it as it is or throw an illegal parameter exception. The specific interface is as follows:
ConfigurablePropertyResolver interface:
This interface defines how the component itself is configured. For example: I just mentioned that when obtaining value, you can specify any type, which depends on the ConversionService for type conversion. The current interface provides the settings and acquisition of ConversionService. In addition, you can configure the format of property placeholders, including: placeholder prefix (default is "${"), placeholder suffix (default is "}"), placeholder value separator (default is ":", used to separate propertyName and defaultValue). The component can also set which attributes must exist, and can also verify whether the attributes that must exist really exist (if they do not exist, an exception will be thrown). The specific interface is as follows:
AbstractPropertyResolver class:
Abstract implementation class for the above two interfaces. It implements all methods of the ConfigurablePropertyResolver interface. Regarding the PropertyResolver interface method, there are 3 getProperty methods that need to be implemented in subclasses (other overloaded methods call these 3 methods):
String getProperty(String key);<T> T getProperty(String key, Class<T> targetType);<T> Class<T> getPropertyAsClass(String key, Class<T> targetType);/*** The current class also defines an additional abstract method to directly return the obtained value value (no placeholder replacement). * The general getProperty method will replace the placeholder in the value value by default and return it. */protected abstract String getPropertyAsRawString(String key); As for replacing property placeholder, it is done with the help of two PropertyPlaceholderHelper property placeholder assistant (tool class) objects, one of these two objects is strict mode and the other is non-strict mode.
PropertySourcesPropertyResolver class:
This class is the only complete implementation class in the system. It takes the PropertySources property source collection (list<PropertySource> internally held property source list) as the source of the property value, iterates through each PropertySource in order, and returns if it obtains a non-null property value.