1、初始化PropertyPlaceholderHelper对象
protected String placeholderPrefix = "${";
protected String placeholderSuffix = "}";
@Nullable
protected String valueSeparator = ":";
private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<>(4);
static {
wellKnownSimplePrefixes.put("}", "{");
wellKnownSimplePrefixes.put("]", "[");
wellKnownSimplePrefixes.put(")", "(");
}
public PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix,
@Nullable String valueSeparator, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(placeholderPrefix, "'placeholderPrefix' must not be null");
Assert.notNull(placeholderSuffix, "'placeholderSuffix' must not be null");
//默认值${
this.placeholderPrefix = placeholderPrefix;
//默认值}
this.placeholderSuffix = placeholderSuffix;
String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix);
//当前缀为空或跟定义的不匹配,取传入的前缀
if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) {
this.simplePrefix = simplePrefixForSuffix;
}
else {
this.simplePrefix = this.placeholderPrefix;
}
//默认值:
this.valueSeparator = valueSeparator;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
2、解析@Value
protected String parseStringValue(
String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
StringBuilder result = new StringBuilder(value);
//是否包含前缀,返回第一个前缀的开始index
int startIndex = value.indexOf(this.placeholderPrefix);
while (startIndex != -1) {
//找到最后一个后缀的index
int endIndex = findPlaceholderEndIndex(result, startIndex);
if (endIndex != -1) {
//去掉前缀后缀,取出里面的字符串
String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
String originalPlaceholder = placeholder;
if (!visitedPlaceholders.add(originalPlaceholder)) {
throw new IllegalArgumentException(
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
}
// 递归判断是否存在占位符,可以这样写${acm.endpoint:${address.server.domain:}}
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
// 根据key获取对应的值
String propVal = placeholderResolver.resolvePlaceholder(placeholder);
// 值不存在,但存在默认值的分隔符
if (propVal == null && this.valueSeparator != null) {
// 获取默认值的索引
int separatorIndex = placeholder.indexOf(this.valueSeparator);
if (separatorIndex != -1) {
// 切掉默认值的字符串
String actualPlaceholder = placeholder.substring(0, separatorIndex);
// 切出默认值
String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
// 根据新的key获取对应的值
propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
// 如果值不存在,则把默认值赋值给当前值
if (propVal == null) {
propVal = defaultValue;
}
}
}
// 如果当前值不为NULL
if (propVal != null) {
// 递归获取存在占位符的值信息
propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
// 替换占位符
result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
if (logger.isTraceEnabled()) {
logger.trace("Resolved placeholder '" + placeholder + "'");
}
startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
}
else if (this.ignoreUnresolvablePlaceholders) {
// Proceed with unprocessed value.
startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
}
else {
throw new IllegalArgumentException("Could not resolve placeholder '" +
placeholder + "'" + " in value \"" + value + "\"");
}
visitedPlaceholders.remove(originalPlaceholder);
}
else {
startIndex = -1;
}
}
return result.toString();
}