gpt4 book ai didi

java - 在 spring 中从 ResourceBundleMessageSource 按模式获取属性键

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:34:49 25 4
gpt4 key购买 nike

我有将近一百个这样的属性

    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

而且我希望能够在事先不了解键的情况下获得这些属性(包括键/值)...类似于 getPropertyPair(regexp .*.order.[a-z]*=)

有人知道 spring 或 JDK 是否为此提供了一些东西吗?我想我将不得不获取 ResourceBundle 并获取所有 key 并对它们进行正则表达式...

最佳答案

我不认为你可以在 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));
}

}

}

这有点 hacky,但我相信您可以将它整理成更有用的东西。

关于java - 在 spring 中从 ResourceBundleMessageSource 按模式获取属性键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4240537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com