使用Class.getResourcesAsStream获取Apache Commons Configuration [英] Using Class.getResourcesAsStream to get Apache Commons Configuration
问题描述
填充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
完成此操作?
推荐答案
如果您将String
传递给它们,则不同容器(例如org.apache.commons.configuration.PropertiesConfiguration
和org.apache.commons.configuration.XMLPropertiesConfiguration
)的构造函数会在内部处理此问题.他们在内部使用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 Configuration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!