In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to load Environment in Spring, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
ConversionService (blue): type conversion service
PropertySource (green): key-value pair data source
PropertyResolver (red): key-value pair services, including type conversion
Environment (purple): environment configuration data service
1.ConversionService
provides a type conversion service, which can convert the source target into the target type, provides management functions, and maintains the mapping relationship of various types of conversion internally. In fact, the function of the module can be seen from the interfaces of ConversionService and ConverterRegistry, as follows:
ConversionService interface is the main external function interface, which provides the ability to query.
The ConverterRegistry interface is the main management interface, providing the ability to add and delete. While ConfigurableConversionService inherits from the above two, it provides the CRUD function of Converter. The structure also continues the inherent style of Spring, the execution interface as the main function to provide a single interface, and then through the way of inheritance, starting with Configurable sub-interface, expand the management function, making the separation of responsibilities more three-dimensional.
is followed by the GenericConversionService class, which provides all the implementations of the interfaces. The following figure shows its main implementation:
GenericConversionService is structurally a small management system with a Converters object internally maintained for the "underlying" management of all GenericConverter. At the same time, a ConcurrentReferenceHashMap is maintained to cache commonly used GenericConverter.
Converters also classifies when storing GenericConverter. If GenericConverter specifies categories that can be parsed (ConvertiablePair: including SourceType and TargetType), a LinkedHashMap is used to store by Key Value. When storing, it traverses the resolvable categories and appends the GenericConverter to the end of the corresponding Value list, so you can see that the Value of the Map is a LinkedList. A GenericConverter that does not specify a category that can be resolved is placed directly in the collection maintained by LinkedHashSet.
Converters traverses the combination of source and target types when querying to find a matching target GenericConverter object. As follows:
For the getRegisteredConverter method, first uses Key to find out if there is a matching Converter in the LinkedHashMap, and then traverses the corresponding Value to find a converter that can be processed. If it cannot be found in the Map, traverse the LinkedHashSet to find the converter that can be processed.
knows that Converters has a process of traversing the list many times when searching, and it will be less efficient when the frequency is too high. Therefore, GenericConversionService maintains a ConcurrentReferenceHashMap to provide caching function internally. This Map provides the same function as ConcurrentHashMap, but can store corresponding soft references, so memory can be reclaimed automatically when memory is insufficient. When the converter is found, it will first try to find it from the cache, if it cannot be obtained, it will look from Converters instead, and when it is found from Converters, it will put into the ConcurrentReferenceHashMap cache.
DefaultConversionService is a singleton that inherits from GenericConversionService and automatically adds default converters after initialization, including Scalar-related, collection-related, and so on.
2.PropertySource
PropertySource represents a data source that contains key-value pairs. In terms of class definition, there is a name field that represents the name of the data source, and a source field that represents the specific data source generic T. The setting of the data source is passed in through the constructor, and the method provides an abstract method getProperty to obtain the key value through the key name. In addition, there are other abstract methods, such as containsProperty.
EnumerablePropertySource inherits from PropertySource and adds the getPropertyNames method, which requires the subclass to return a list of key names held in memory. At the same time, the containsProperty method is implemented to determine whether the key name is included by judging whether the key name exists in the list of key names returned above.
MapPropertySource inherits from EnumerablePropertySource and, as its name implies, internally maintains key-value pairs of content through Map. Similarly, PropertiesPropertySource maintains key-value pairs of content internally through Properties.
SystemEnvironmentPropertySource is the decorator of MapPropertySource, which inherits from MapPropertySource and adds the function of key-name conversion to deal with the environment of environment variables and shell parameters. When you get the key value through the key name, you will first look for it according to the original key name. If you cannot find it, you will convert the key and then try to find it. The specific search process is as follows:
Find through name
In the name. Convert to _ find
Convert-in name to _ lookup
In the name. And _ convert to-find
Convert name to uppercase, and then proceed to (1)-(4)
PropertySources is implemented as follows, extending the PropertySource interface to extend the capabilities of a single data source to multiple. As the implementation of PropertySources, MutablePropertySources maintains a List object internally to store multiple data sources and encapsulates its own behavior as List.
3.PropertyResolver
PropertyResolver defines a series of interfaces to provide the function of obtaining the corresponding value according to the key name, as well as the function of type conversion and placeholder replacement, which is the combination of ConversionService and PropertySource. The ConfigurablePropertyResolver interface inherits from the PropertyResolver interface, the old rules, and extends the function of settings, mainly setting the relevant properties of type converters and placeholders.
AbstractPropertyResolver provides other implementations in addition to the PropertySource functionality. Use DefaultConversionService as the default type conversion implementation, use ${and} as the prefix and suffix of the placeholder, use: as the default value separator, and introduce PropertyPlaceholderHelper for parsing and replacing placeholders. The implementation of getProperty is left in the subclass PropertySourcesPropertyResolver, which introduces PropertySources to maintain multiple key-value pairs of data sources. The procedure for getting the specified attribute value is as follows:
traverses the data source, and after finding the corresponding value, the placeholder is replaced, and the type is converted after the placeholder is replaced. DefaultConversionService is used directly for type conversion, which has been introduced above, and placeholder substitution is described below.
The function of placeholder replacement is defined in the PropertyResolver API and is divided into strict and non-strict modes, as follows:
resolvePlaceholders is in loose mode. If the placeholder cannot be replaced, it will be ignored directly. ResolveRequiredPlaceholders is in strict mode. If the placeholder cannot be replaced, an exception will be thrown. As mentioned above, AbstractPropertyResolver implementations are delegated to PropertyPlaceholderHelper's replacePlaceholders method.
as above, this method requires that you pass in a source string and provide a PlaceholderResolver data source, which can get the corresponding value from the data source after parsing the placeholder content at once. In order to maintain the single responsibility of the class function, an internal interface PlaceholderResolver is added. As mentioned above, all the key-value pairs in this module are maintained by PropertySourcesPropertyResolver. In fact, in the implementation of the screenshot of the above method, the getPropertyAsRawString method is indeed implemented by PropertySourcesPropertyResolver. Let's take a look at the placeholder parsing.
The parsing process of the placeholder is as follows, and the main process is:
Get startIndex based on the ${prefix
Find the} suffix paired with the ${prefix, such as ${xxx$ {yy} z}, and get the subscript endIndex of the second} suffix
Intercept the contents between ${and} to get placeholder
Since the content of the placeholder may also contain placeholders, the placeholder should be processed recursively. Since the placeholders can be nested, the results of the inner layer can be used as the outer Key.
After parsing the placeholder, use it as Key to get the corresponding value propVal from the source of the key-value pair.
If the propVal value is empty, determine whether it exists: a delimiter, if any, split it, and use the front-end content as the Key to look for the value again. If the search result is not empty, the value of the result is propVal, otherwise the second paragraph is used as the default value.
If the propVal result in step (5) / (6) is not empty, determine whether the value obtained from the key-value pair source also has a placeholder. If there is a placeholder, parse it again. If not, replace the result back to the original field, update the startIndex, and continue the next parsing.
If the propVal result in step (5) / (6) is not empty, the next step will be judged according to the parsing mode set. If not, skip the content, update the startIndex, and proceed to the next parsing. If it is strict mode, an exception will be thrown and the process ends.
is demonstrated with an example below, as follows
The output is as follows:
throws an exception if the parsing mode is set to strict mode
4.Environment
Environment inherits from the PropertyResolver interface and adds the Profiles function, that is, the multi-environment feature we usually see, which can load different configurations in different environments. ConfigurableEnvironment inherits from Environment, the old rule, adds a modified extension interface, and adds an interface to get system parameters. In addition, the interface is also inherited from ConfigurablePropertyResolver, with the ability to manage, obtain and process data sources with key values, and integrate the functions of Environment interface to achieve the effect of environment isolation by loading different configuration sources in different environments.
AbstractEnvironment is the implementation of ConfigurablePropertyResolver, which provides the default environment source default, while the internal combination uses PropertySourcesPropertyResolver as the implementation of PropertyResolver.
also maintains a MutablePropertySources object for storing multiple data sources, and in the parent-child context of Context, the contents of the environment variables in the parent above can be added through the merge method (when AbstractApplicationContext sets the parent Context, the parent Environment is merged). At the same time, there is also a method customizePropertySources, which is called in the constructor, and the default key-value pair source is added to the subclass, as follows:
Finally, is the StandardEnvironment class, which inherits from AbstractEnvironment and overrides the customizePropertySources method, which adds key-value pair sources for system-related properties and application environment variable-related properties. As follows
and these two data sources come from the PropertySource implementation mentioned earlier. Among them, the data source of the system related attribute SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME comes from System.getProperties (), while the application environment variable related attribute SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME comes from System.getenv ().
This is how to load Environment in Spring. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.