1.先描述业务场景:
在Apollo上做配置,然后读取配置,然后做替换
2.一路坎坷:
2.1.在Apollo上配置,但是vlue包含有占位符“${}”,获取配置报错;
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'loanName' in value
2.2.去掉$后,不能替换
3.最终代码
public static final char DEFAULT_ESCAPE = '$';
/**
* Constant for the default variable prefix.
*/
public static final StrMatcher DEFAULT_PREFIX = StrMatcher.stringMatcher("{");
/**
* Constant for the default variable suffix.
*/
public static final StrMatcher DEFAULT_SUFFIX = StrMatcher.stringMatcher("}");
public static void main(String[] args) {
Map<Object, Object> paramMap = Maps.newHashMap();
paramMap.put("username", "张三");
paramMap.put("loanName", "手机银行");
StrSubstitutor strSubstitutor = new StrSubstitutor(StrLookup.mapLookup(paramMap), DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
String message = strSubstitutor.replace("姓名:{username}, 产品名称:'{loanName}");
System.out.println("message= " + message);
}