在Spring中从ResourceBundleMessageSource获取属性键

lis*_*sak 3 java spring properties

我有几百个这样的房产

    NotEmpty.order.languageFrom=Field Language can't be empty
    NotEmpty.order.languageTo=Field Language can't be empty
    NotEmpty.order.description=Description field can't be empty
    NotEmpty.order.formType=FormType field can't be empty
    NotEmpty.cart.formType=FormType field can't be empty
    NotEmpty.cart.formType=FormType field can't be empty
Run Code Online (Sandbox Code Playgroud)

而且我希望能够获得这些属性(键/值),而无需事先了解密钥......就像 getPropertyPair(regexp .*.order.[a-z]*=)

有人知道spring或JDK是否提供了相应的东西吗?我想我必须得到ResourceBundle并获取所有密钥并正则表达它们......

Gar*_*owe 6

我不认为你可以在Spring中做到这一点,但这里有一些可能有用的代码:

public class Main {
  public static void main(String[] args) {
    ResourceBundle labels = ResourceBundle.getBundle("spring-regex/regex-resources", Locale.UK);
    Enumeration<String> labelKeys = labels.getKeys();

    // Build up a buffer of label keys
    StringBuffer sb = new StringBuffer();
    while (labelKeys.hasMoreElements()) {
      String key = labelKeys.nextElement();
      sb.append(key + "|");
    }

    // Choose the pattern for matching
    Pattern pattern = Pattern.compile(".*.order.[a-z]*\\|");
    Matcher matcher = pattern.matcher(sb);

    // Attempt to find all matching keys
    List<String> matchingLabelKeys = new ArrayList<String>();
    while (matcher.find()) {
      String key=matcher.group();
      matchingLabelKeys.add(key.substring(0,key.length()-1));
    }

    // Show results
    for (String value: matchingLabelKeys) {
      System.out.format("Key=%s Resource=%s",value,labels.getString(value));
    }

  }

}
Run Code Online (Sandbox Code Playgroud)

这有点hacky但我相信你可以把它整理成更有用的东西.