1. 程式人生 > >Spring屬性解析器PropertySourcesPropertyResolver

Spring屬性解析器PropertySourcesPropertyResolver

概述

Spring框架將某個屬性源抽象成了類PropertySource,又將多個屬性源PropertySource抽象為介面PropertySources。對某個PropertySource物件中屬性的解析,抽象成了介面PropertyResolver,而類PropertySourcesPropertyResolver則是Spring用於解析一個PropertySources物件中屬性的工具類。Spring應用Environment物件中對其PropertySources物件的屬性解析,就是通過這樣一個物件。

// Spring Environment 實現類的抽象基類 AbstractEnvironment程式碼片段
private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);

原始碼解析

package org.springframework.core.env;

import org.springframework.lang.Nullable;

/**
 * PropertyResolver implementation that resolves property values against
 * an underlying set of PropertySources.
 * 一個PropertyResolver實現類,用於解析一個PropertySources物件中的屬性源集合中的屬性。
 * 
 * 這裡PropertySourcesPropertyResolver繼承自基類AbstractPropertyResolver,
 * AbstractPropertyResolver提供了很多方法實現,不過這裡不做過多解析,而僅僅關注
 * 如何獲取一個屬性的值的邏輯。
 * 
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.1
 * @see PropertySource
 * @see PropertySources
 * @see AbstractEnvironment
 */
public class PropertySourcesPropertyResolver extends AbstractPropertyResolver { // PropertySources形式存在的屬性源集合,該工具的工作物件 @Nullable private final PropertySources propertySources; /** * Create a new resolver against the given property sources. * @param propertySources the set of PropertySource objects to use */
public PropertySourcesPropertyResolver(@Nullable PropertySources propertySources) { this.propertySources = propertySources; } // 檢視是否包含某個指定名稱的屬性,判斷方法: // 1. 底層任何一個屬性源包含該屬性的話就認為是包含; // 2. 否則認為是不包含。 @Override public boolean containsProperty(String key) { if (this.propertySources != null) { for (PropertySource<?> propertySource : this.propertySources) { if (propertySource.containsProperty(key)) { return true; } } } return false; } // 獲取某個指定名稱的屬性的值,如果該屬性不被包含的話,返回null // 不管該屬性的值是什麼型別,將它轉換成字串型別 @Override @Nullable public String getProperty(String key) { return getProperty(key, String.class, true); } // 獲取某個指定名稱的屬性的值,並將其轉換成指定的型別,如果該屬性不被包含的話,返回null @Override @Nullable public <T> T getProperty(String key, Class<T> targetValueType) { return getProperty(key, targetValueType, true); } // 獲取某個指定名稱的屬性的值,如果該屬性不被包含的話,返回null // 不管該屬性的值是什麼型別,將它轉換成字串型別 @Override @Nullable protected String getPropertyAsRawString(String key) { return getProperty(key, String.class, false); } // 獲取某個指定名稱的屬性的值,並將其轉換成指定的型別,如果該屬性不被包含的話,返回null // resolveNestedPlaceholders引數指示是否要解析屬性值中包含的佔位符 @Nullable protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) { if (this.propertySources != null) { // 遍歷每個屬性源,如果發現目標屬性被某個屬性源包含,則獲取它的值並按要求做相應的處理然後返回處理 // 後的值從這裡使用for迴圈的方式來看,可以將屬性源看作是一個List,索引較小的屬性源先被訪問,也就 // 是說,索引較小的屬性源具有較高優先順序 for (PropertySource<?> propertySource : this.propertySources) { if (logger.isTraceEnabled()) { logger.trace("Searching for key '" + key + "' in PropertySource '" + propertySource.getName() + "'"); } Object value = propertySource.getProperty(key); if (value != null) { if (resolveNestedPlaceholders && value instanceof String) { // 解析值中的佔位符 value = resolveNestedPlaceholders((String) value); } logKeyFound(key, propertySource, value); // 根據要求做相應的型別轉換然後返回轉換後的值 return convertValueIfNecessary(value, targetValueType); } } } if (logger.isTraceEnabled()) { logger.trace("Could not find key '" + key + "' in any property source"); } // 任何屬性源中都不包含該屬性,返回null return null; } /** * Log the given key as found in the given PropertySource, resulting in * the given value. * The default implementation writes a debug log message with key and source. * As of 4.3.3, this does not log the value anymore in order to avoid accidental * logging of sensitive settings. Subclasses may override this method to change * the log level and/or log message, including the property's value if desired. * @param key the key found * @param propertySource the PropertySource that the key has been found in * @param value the corresponding value * @since 4.3.1 */ protected void logKeyFound(String key, PropertySource<?> propertySource, Object value) { if (logger.isDebugEnabled()) { logger.debug("Found key '" + key + "' in PropertySource '" + propertySource.getName() + "' with value of type " + value.getClass().getSimpleName()); } } }