如何:在 Java 中动态定义属性

发布于 2024-09-07 05:31:35 字数 113 浏览 3 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧城空念 2024-09-14 05:31:35

继续使用 .properties 并将文件作为资源加载。

如果它在类路径中,就会找到它。

我使用的是资源包,因为它对我来说更容易。

编辑

如果该文件位于您的类路径中,您可以使用以下命令将其作为资源加载:Some.class.loadResource(...) 或者我所做的是使用 ResourceBundle其作用基本相同。

例如,如果我有:

import java.util.ResourceBundle;

public class ResourceBundleTest {
    public static void main( String [] args ) {
        ResourceBundle bundle = ResourceBundle.getBundle("connection");
        for( String key: bundle.keySet() ){
            System.out.printf("bundle[%s]=%s%n",key, bundle.getString(key));
        }
    }
}

如果该文件位于类路径中,我可以加载该文件。该属性位于外部,在“some/nested/dir”中

$ls -l some/nested/dir/
total 8
-rw-r--r--  1 oscarreyes  staff  35 Jun 25 12:06 connection.properties
$cat some/nested/dir/connection.properties 
name=Oscar
lastName=Reyes
age=0x1F

如果我运行它而不将该目录添加到我的类路径中,它将无法工作:

$java ResourceBundleTest 
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name connection, locale es_ES
    at java.ut...ceBundle.java:1427)
    at java.ut...urceBundle.java:1250)
    at java.ut...ceBundle.java:705)
    at Resourc...st.java:6)

但是如果我将该目录添加到我的类路径中,那么将很容易找到该文件。

$java -cp some/nested/dir/:.  ResourceBundleTest 
bundle[lastName]=Reyes
bundle[age]=0x1F
bundle[name]=Oscar
$

以类似的方式,您可以拥有一个 .jar 文件,并将 .properties 文件放在您想要的任何位置,只需将其包含在类路径中即可。

等效的使用属性是:

import java.util.Properties;

public class LoadProperties {
    public static void main( String [] args ) throws java.io.IOException {
        Properties properties = new Properties();
        properties.load( LoadProperties.class.getResourceAsStream("connection.properties"));
        properties.list( System.out );
    }
}

但出于某种原因,我更喜欢资源包。

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:

import java.util.ResourceBundle;

public class ResourceBundleTest {
    public static void main( String [] args ) {
        ResourceBundle bundle = ResourceBundle.getBundle("connection");
        for( String key: bundle.keySet() ){
            System.out.printf("bundle[%s]=%s%n",key, bundle.getString(key));
        }
    }
}

I can load that file if is in the class path. The property is outside, in "some/nested/dir"

$ls -l some/nested/dir/
total 8
-rw-r--r--  1 oscarreyes  staff  35 Jun 25 12:06 connection.properties
$cat some/nested/dir/connection.properties 
name=Oscar
lastName=Reyes
age=0x1F

If I run it without adding that directory to my classpath it wont work:

$java ResourceBundleTest 
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name connection, locale es_ES
    at java.ut...ceBundle.java:1427)
    at java.ut...urceBundle.java:1250)
    at java.ut...ceBundle.java:705)
    at Resourc...st.java:6)

But if I add the directory to my classpath, then the file will be found easily.

$java -cp some/nested/dir/:.  ResourceBundleTest 
bundle[lastName]=Reyes
bundle[age]=0x1F
bundle[name]=Oscar
$

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:

import java.util.Properties;

public class LoadProperties {
    public static void main( String [] args ) throws java.io.IOException {
        Properties properties = new Properties();
        properties.load( LoadProperties.class.getResourceAsStream("connection.properties"));
        properties.list( System.out );
    }
}

But for some reason I prefer the resource bundle.

萝莉病 2024-09-14 05:31:35

只需将文件放入运行时类路径覆盖的路径之一,或者将文件的路径添加到(默认)运行时类路径。这样 Java 代码就可以继续将其作为类路径资源进行访问。

当您使用 java -jar 命令或双击文件执行 JAR 文件时,-cp-classpath 参数甚至 %CLASSPATH% 环境变量将被忽略。您可以将 JAR 文件的默认类路径定义为 META-INF/MANIFEST.MF 文件中的 Class-Path 条目。

例如,

Class-Path: .

. 表示 JAR 文件当前所在的路径包含在运行时类路径中。因此,如果将属性文件放在与 JAR 文件相同的文件夹中,它就会起作用。

您还可以指定绝对磁盘文件系统路径:

Class-Path: /var/foo

这样,文件夹 /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 as Class-Path entry in META-INF/MANIFEST.MF file.

E.g.

Class-Path: .

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:

Class-Path: /var/foo

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.

萌梦深 2024-09-14 05:31:35

属性文件很好,您只需将其包含在类路径中,并且它可以存在于 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.

驱逐舰岛风号 2024-09-14 05:31:35

是的,使用属性文件是常见的做法。此外,一般做法是为每个环境提供单独的属性文件,构建过程将根据目标选择正确的属性文件。您需要做的就是将文件保存在类路径中并使用类加载器来查找它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文