使用 Class.getResourcesAsStream 获取 Apache Commons 配置 [英] Using Class.getResourcesAsStream to get Apache Commons Configuration

查看:23
本文介绍了使用 Class.getResourcesAsStream 获取 Apache Commons 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

填充 java.util.Properties 对象的理想方法似乎是使用一些变体

The ideal way to populate a java.util.Properties object seems to be by using some variation on

properties.load(ClassLoader.getSystemClassLoader().getResourcesAsStream(String className));

重要的想法是将 Properties.load 指向类的 InputStream,而不是路径.

The big idea being to point Properties.load at a InputStream of a class, rather than at a path.

如何使用 org.apache.commons.configuration.Configuration 做到这一点?

How can this be done using org.apache.commons.configuration.Configuration?

推荐答案

不同容器的构造器(例如 org.apache.commons.configuration.PropertiesConfigurationorg.apache.commons.configuration.XMLPropertiesConfiguration) 如果你给它们传递一个 String,内部会处理这个问题.他们在内部使用 org.apache.commons.configuration.ConfigurationUtils 中的 API 来尝试在 用户主目录、当前类路径和系统类路径.

The constructors of the different containers (e.g. org.apache.commons.configuration.PropertiesConfiguration and org.apache.commons.configuration.XMLPropertiesConfiguration) internally take care of this if you pass them a String. They internally use the API in org.apache.commons.configuration.ConfigurationUtils to try and find the resource in the user home directory, the current classpath and the system classpath.

顺便说一句,您可能会发现这篇文章在以下情况下很有用试图找出从类路径加载属性文件时要使用的正确"类加载器.

Incidentally, you might find this article useful when trying to figure out the "correct" class loader to use when loading properties files from the classpath.

这篇关于使用 Class.getResourcesAsStream 获取 Apache Commons 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆