你可以用这个替换 DefaultPropertiesPersister:
这将允许您引用其他条目,例如:
user=User
user.add=Add ${user}
user.delete=Delete ${user}
只需使用您的 MessageSource 指定此持久性,例如messageSource.setPropertiesPersister(new RecursivePropertiesPersister());
来源:
public class RecursivePropertiesPersister extends DefaultPropertiesPersister {
private final static Pattern PROP_PATTERN = Pattern.compile("\\$'?\\{'?([^}']*)'?\\}'?");
private final static String CURRENT = "?LOOP?";
@Override
public void load(Properties props, Reader reader) throws IOException {
Properties propsToLoad = new Properties();
super.load(propsToLoad, reader);
replace(propsToLoad, props);
}
@Override
public void load(Properties props, InputStream is) throws IOException {
Properties propsToLoad = new Properties();
super.load(propsToLoad, is);
replace(propsToLoad,props);
}
protected void replace ( Properties src, Properties dest) {
for (Map.Entry entry: src.entrySet()) {
String key = (String) entry.getKey();
String value = (String)entry.getValue();
replace(src, dest, key, value);
}
}
protected String replace(Properties src, Properties dest, String key, String value) {
String replaced = (String) dest.get(key);
if (replaced != null) {
// already replaced (or loop), just return the string
return replaced;
}
dest.put(key,CURRENT); // prevent loops
final Matcher matcher = PROP_PATTERN.matcher(value);
final StringBuffer sb = new StringBuffer();
while (matcher.find()) {
final String subkey = matcher.group(1);
final String replacement = (String)src.get(subkey);
matcher.appendReplacement(sb,replace(src,dest,subkey,replacement));
}
matcher.appendTail(sb);
final String resolved = sb.toString();
dest.put(key, resolved);
return resolved;
}
}