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

查看:131
本文介绍了使用Class.getResourcesAsStream获取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.PropertiesConfigurationorg.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屋!

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