- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用以下代码在 Java 8 中加载 TestResourceBundle.properties 文件时:
ResourceBundle resources = ResourceBundle.getBundle("TestResourceBundle");
我意识到,如果类路径中存在一个同名的 ListResourceBundle 类(TestResourceBundle.class),如下所示:
public class TestResourceBundle extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
private Object[][] contents = {
{ "Test", "Value2" }
};
}
ListResourceBundle 类的优先级始终高于属性文件,最后加载 ListResourceBundle 类。
这是标准行为吗?
如果我想加载一个属性文件资源包,并且不知道类路径中是否存在同名的ListResourceBundle,该怎么办?
如何确保始终加载属性文件而不是同名的资源包类文件?
最佳答案
在 ResourceBundle.Control 类中,有一个 FORMAT_DEFAULT 列表,该列表始终为类资源包提供比属性文件更高的优先级:
public static final List<String> FORMAT_DEFAULT
= Collections.unmodifiableList(Arrays.asList("java.class",
"java.properties"));
我们可以更改优先级顺序,覆盖 ResourceBundle.Control 类的 getFormats() 方法,如以下代码所示:
ResourceBundle resources = ResourceBundle.getBundle("TestResourceBundle", new ResourceBundle.Control() {
@Override
public List<String> getFormats(String baseName) {
if (baseName == null) {
throw new NullPointerException();
}
return Collections.unmodifiableList(Arrays.asList("java.properties", "java.class"));
}
});
关于java - PropertyResourceBundle 与 ListResourceBundle 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35015384/
在 ListResourceBundle 实现中,我定义了一个数组,其中一些键包含空值。它可以很好地编译包含空值的包的加载过程。但是,当我在具有非空值的键上调用 getObject 时(如下面的示例“
我正在从事一个涉及加载 ResourceBundle 的项目。更明确地说,我创建了一个扩展 ListResourceBundle 的类。该类称为 Resources.java。它编译一切正常,但每次我
尝试使用以下代码在 Java 8 中加载 TestResourceBundle.properties 文件时: ResourceBundle resources = ResourceBundle.ge
我是一名优秀的程序员,十分优秀!