如何:在 Java 中动态定义属性
如何在 Java 中动态定义一些属性。目前,我正在使用属性文件,但我需要在安装之前更改这些属性,因此这些属性应该在 jar(或安装)文件外部的文件中设置。
这些属性定义了我的 IBatis 连接。
How can I define some properties dynamically in Java. For now, I'm using a properties file, but I need to change these properties before installation, so these properties should be set in a file outside the jar (or install) file.
These properties define my IBatis connection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
继续使用 .properties 并将文件作为资源加载。
如果它在类路径中,就会找到它。
我使用的是资源包,因为它对我来说更容易。
编辑
如果该文件位于您的类路径中,您可以使用以下命令将其作为资源加载:
Some.class.loadResource(...)
或者我所做的是使用 ResourceBundle其作用基本相同。例如,如果我有:
如果该文件位于类路径中,我可以加载该文件。该属性位于外部,在“some/nested/dir”中
如果我运行它而不将该目录添加到我的类路径中,它将无法工作:
但是如果我将该目录添加到我的类路径中,那么将很容易找到该文件。
以类似的方式,您可以拥有一个 .jar 文件,并将 .properties 文件放在您想要的任何位置,只需将其包含在类路径中即可。
等效的使用属性是:
但出于某种原因,我更喜欢资源包。
Keep going with the .properties and load the file as a resource.
If it is in the classpath it would be found.
What I use, because it is much easier to me is a resource bundle instead.
edit
If the file is in your classpath you can loaded it either as a resource with:
Some.class.loadResource(...)
or what I do is use a ResourceBundle which does basically the same.For instance if I have:
I can load that file if is in the class path. The property is outside, in "some/nested/dir"
If I run it without adding that directory to my classpath it wont work:
But if I add the directory to my classpath, then the file will be found easily.
In a similar fashion, you can have a .jar file, and put your .properties file wherever you want, you just have to include it in your classpath.
The equivalent using properties would be:
But for some reason I prefer the resource bundle.
只需将文件放入运行时类路径覆盖的路径之一,或者将文件的路径添加到(默认)运行时类路径。这样 Java 代码就可以继续将其作为类路径资源进行访问。
当您使用
java -jar
命令或双击文件执行 JAR 文件时,-cp
或-classpath
参数甚至%CLASSPATH%
环境变量将被忽略。您可以将 JAR 文件的默认类路径定义为META-INF/MANIFEST.MF
文件中的Class-Path
条目。例如,
点
.
表示 JAR 文件当前所在的路径包含在运行时类路径中。因此,如果将属性文件放在与 JAR 文件相同的文件夹中,它就会起作用。您还可以指定绝对磁盘文件系统路径:
这样,文件夹
/var/foo
就会出现在运行时类路径中。因此,如果您将属性文件放入该文件夹中,它就会起作用。Just put the file in one of the paths covered by the runtime classpath, or to add the file's path to the (default) runtime classpath. This way the Java code can continue accessing it as classpath resource.
When you execute JAR files using
java -jar
command or by doubleclicking the file, the-cp
or-classpath
arguments and even the%CLASSPATH%
environment variable will be ignored. You can define the JAR file's default classpath asClass-Path
entry inMETA-INF/MANIFEST.MF
file.E.g.
The dot
.
means that the path where the JAR file is currently sitting is included in the runtime classpath. So if you put the properties file in the same folder as the JAR file, it'll work.You can also specify an absolute disk file system path:
This way the folder
/var/foo
is taken in the runtime classpath. So if you put the properties file in that folder, it'll work.属性文件很好,您只需将其包含在类路径中,并且它可以存在于 jar 之外。
如果您想要更高级,您可以使用首选项 API,但是虽然这将使事情更加符合操作系统的标准,但它可能会使部署变得复杂,因为根据您的平台,您的配置将有不同的位置,并且需要依赖于平台的安装。
A properties file is fine, you just have to include it in the classpath and it can live outside the jar.
If you want to get fancier, you have the Preferences API, but while that will keep things more to the standard of your operating system, it can make deployment complicated as you will have different locations for your configuration depending on your platform, and will require a platform dependent install.
是的,使用属性文件是常见的做法。此外,一般做法是为每个环境提供单独的属性文件,构建过程将根据目标选择正确的属性文件。您需要做的就是将文件保存在类路径中并使用类加载器来查找它。
Yes, Using a properties file is common practice. In addition the general practice is to have separate properties file for each environment and the build process will pick the right one depending on the target. All you need to do is keep the file in classpath and use classloader to find it.