当前位置: 首页 > 面试题库 >

是否可以在资源束中包含资源束文件

支智志
2023-03-14
问题内容

我们正在使用java.util.ResourceBundle加载属性信息。我们的属性文件已经变得非常庞大,我们正在考虑将主属性文件分成几个子模块。有可能实现这一目标吗?

master.properties

==>

 master.properties
   include moduleA.properties
   include moduleB.properties

让我知道?


问题答案:

首先,我想知道为什么您选择java.util.ResourceBundlejava.util.Properties。给出问题的表达方式后,您似乎不必关心本地化/国际化或捆绑文件继承。

有了Properties它,它就变得异常容易,因为它实现Map了反过来又提供了putAll()一种合并另一张地图的方法。开球示例:

Properties master = new Properties();
master.load(masterInput);

Properties moduleA = new Properties();
moduleA.load(moduleAinput);
master.putAll(moduleA);

Properties moduleB = new Properties();
moduleB.load(moduleBinput);
master.putAll(moduleB);

// Now `master` contains the properties of all files.

如果您真的坚持使用ResourceBundle,则最好的选择是创建一个自定义ResourceBundle,在该自定义中使用自定义控制加载Control

假设您具有以下条目,master.properties其中表示一个以逗号分隔的字符串,带有模块属性文件的基本名称:

include=moduleA,moduleB

然后,以下自定义ResourceBundle示例应该起作用:

public class MultiResourceBundle extends ResourceBundle {

    protected static final Control CONTROL = new MultiResourceBundleControl();
    private Properties properties;

    public MultiResourceBundle(String baseName) {
        setParent(ResourceBundle.getBundle(baseName, CONTROL));
    }

    protected MultiResourceBundle(Properties properties) {
        this.properties = properties;
    }

    @Override
    protected Object handleGetObject(String key) {
        return properties != null ? properties.get(key) : parent.getObject(key);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Enumeration<String> getKeys() {
        return properties != null ? (Enumeration<String>) properties.propertyNames() : parent.getKeys();
    }

    protected static class MultiResourceBundleControl extends Control {
        @Override
        public ResourceBundle newBundle(
            String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
                throws IllegalAccessException, InstantiationException, IOException
        {
            Properties properties = load(baseName, loader);
            String include = properties.getProperty("include");
            if (include != null) {
                for (String includeBaseName : include.split("\\s*,\\s*")) {
                    properties.putAll(load(includeBaseName, loader));
                }
            }
            return new MultiResourceBundle(properties);
        }

        private Properties load(String baseName, ClassLoader loader) throws IOException {
            Properties properties = new Properties();
            properties.load(loader.getResourceAsStream(baseName + ".properties"));
            return properties;
        }
    }

}

(忽略异常处理和本地化处理,这取决于您)

可以用作:

ResourceBundle bundle = new MultiResourceBundle("master");


 类似资料:
  • 问题内容: 我正在构建一个jar,以便其他开发人员可以使用我在其Android应用程序中创建的视图。我想知道是否有可能在jar中使用布局资源,以便可以构建视图的布局,或者是否需要以编程方式构建视图。同样,我可以在jar文件中包含图像资源吗? 问题答案: 不,目前不行。由于Android将资源打包到apk中的方式,您不能将它们包含在jar中。但是,您仍然可以与其他开发人员共享jar,他们只需要手动拉

  • 这里列出了 Python 相关的一些资源,欢迎读者补充。 vinta/awesome-python: A curated list of awesome Python frameworks, libraries, software and resources 包含了 Python 框架、Python 库和软件的 awesome 列表。 aosabook/500lines: 500 Lines or

  • 问题内容: 我在android上的自定义视图上写。它使用了一些png,作为项目中drawable- folder(我现在将其放在所有drawable文件夹中,只是为了确保)的资源提供。 不幸的是,当我启动该应用程序时,我被强制关闭,日志最后说的是:(十六进制数字是我打算加载的第一个图标)。 现在让我们感到奇怪的是:eclipse adt插件的图形布局编辑器在将显示图标加载到其设计视图中没有任何问题

  • 在Quarkus中,要将随机文件包含为类路径资源,我们使用<code>Quarkus.native.resources。包括(https://quarkus.io/guides/writing-native-applications-tips#including-资源)。 如何在文件系统中包含文件?即使用读取的内容。 如果我们使用Jib,我们只需将这些文件放在< code>src/main/jib

  • 我在一个文件夹中有一个robot资源文件,并且希望在执行Maven安装时同时包含该文件夹和robot资源文件。 项目>src>main>java... 在我的pom.xml文件中,我添加了: 当我执行Maven install时,我只看到jar中的robot资源文件,而看不到它的文件夹。我如何使它使机器人资源文件在文件夹内?我是Maven的新手,所以我希望得到任何指导。我看过这篇文章,但这篇文章没

  • 问题内容: 我有一些可怕的行为。我有以下Maven配置: 这将创建包含所有类的JAR文件,但我在类路径中有一些资源也应包括在内。如果我将参数放在阶段的前面,那么我的所有资源都会包括在内。如果我使用运行Maven ,则不包含任何资源。我的资源位于(不是通常的目录)下。 问题答案: 目标配置中未正确定义资源的源目录。同样,outputDirectory元素将资源放置在dir中,默认情况下将其打包。试试