**
1.接口 ReplaceVariableService
**
public interface ReplaceVariableService {
String relpacePlaceholader(String source);
}
可传实现类用到的参数,source代表要替换的路径,例如:{{ip}}{{port}} =替换成===》ipport
可含有一个或多个
2.实现类 ReplaceVariable
@Service
public class ReplaceVariable implements ReplaceVariableService{
//前缀以及后缀 参数可写成 想要根据什么替换
private static final String pre="{{";
private static final String suff="}}";
private PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper(pre,suff);;
public String relpacePlaceholader(String source){
return propertyPlaceholderHelper.replacePlaceholders(source,placeholderName -> {
try {
return placeholderName == null ? "值为空!" : placeholderName;
} catch (DataException e) {
e.printStackTrace();
}
return "";
});
}
**
3.可直接调用
String suffixPath = replaceVariable.relpacePlaceholader("{{ip}}port{{aa}}");