在Java中存储应用程序的conf数据的正确方法是什么?

时间:2022-08-24 22:17:05

where do you store user-specific and machine-specific runtime configuration data for J2SE application?

在哪里为J2SE应用程序存储特定于用户和机器的运行时配置数据?

(For example, C:\Users\USERNAME\AppData\Roaming\ on Windows and /home/username on Unix)

(例如,Windows上的C:\ Users \ USERNAME \ AppData \ Roaming \和Unix上的/ home / username)

How do you get these locations in the filesystem in platform-independent way?

如何以与平台无关的方式在文件系统中获取这些位置?

Thanks for your advice!

谢谢你的建议!

6 个解决方案

#1


13  

That depends on your kind of J2SE Application:

这取决于您的J2SE应用程序类型:

  • J2SE executable JAR file (very simple): use user.home System property to find home-dir. Then make a subdir accordingly (like e.g. PGP, SVN, ... do)
  • J2SE可执行JAR文件(很简单):使用user.home系统属性查找home-dir。然后相应地制作一个子目录(例如PGP,SVN,......)

  • Java Web Start provides very nice included methods to safe properties. Always user-specific
  • Java Web Start为安全属性提供了非常好的包含方法。始终是用户特定的

  • Finally Eclipse RCP: There you have the notion of the workspace (also derived from user.home) for users and configuration (not totally sure how to access that tricky in Vista) for computer wide usage
  • 最后Eclipse RCP:你有工作区的概念(也来自user.home)用于用户和配置(不完全确定如何在Vista中访问那些棘手的东西)以供计算机广泛使用

All these approaches are, when used with care -- use correct separatorChar -- OS neutral.

所有这些方法,在小心使用时 - 使用正确的separatorChar - OS中性。

#2


25  

First on the format:

首先是格式:

  1. Java property files are good for key/value pairs (also automatically handle the newline chars). A degree of structure is possible by using 'dot notation'. Drawback is that the structure does not allow you to easily enumerate top-level configuration entities and work in drill-down manner. Best used for a small set of often tweakable environment-specific settings
  2. Java属性文件适用于键/值对(也自动处理换行符)。使用“点符号”可以实现一定程度的结构。缺点是该结构不允许您轻松枚举*配置实体并以深入的方式工作。最适用于一小组经常可调整的环境特定设置

  3. XML files - quite often used for more complex configuration of various Java frameworks (notably J2EE and Spring). I would advice that you at least learn about Spring - it contains many ideas worth knowing even if you decide not to use it. If you decide to roll your own XML configuration, I'd recommend using XStream with customized serialization options or if you just need to parse some XML, take a look at XOM. BTW Spring also allows you to plug your custom XML configuration language, but it's a relatively complex task. XML configuration is best used for more complex 'internal' configuration that is not seen or tweaked by the end user.
  4. XML文件 - 经常用于各种Java框架(尤其是J2EE和Spring)的更复杂配置。我建议您至少要了解Spring - 它包含许多值得了解的想法,即使您决定不使用它。如果您决定推出自己的XML配置,我建议使用XStream和自定义序列化选项,或者如果您只需要解析一些XML,请查看XOM。 BTW Spring还允许您插入自定义XML配置语言,但这是一项相对复杂的任务。 XML配置最适用于最终用户未看到或调整的更复杂的“内部”配置。

  5. Serialized Java objects - a quick and easy way to persist the state of an object and restore it later. Useful if you write a configuration GUI and you don't care if the configuration is human readable. Beware of compatibility issues when you evolve classes.
  6. 序列化Java对象 - 一种快速简便的方法,可以持久保存对象的状态并在以后恢复。如果您编写配置GUI并且不关心配置是否可读,则非常有用。在进化类时要注意兼容性问题。

  7. Preferences - introduced in Java 1.4, allow you to store typed text, numbers, byte arrays and other primitives in platform-specific storage. On Windows, that is the registry (you can choose between /Software/JavaSoft/Prefs under HKLM or HKCU). Under Unix the same API creates files under the user home or /etc. Each prefs hive can be exported and imported as XML file. You can specify custom implementation of the PreferencesFactory interface by setting the "java.util.prefs.PreferencesFactory" JVM property to your implementation class name.
  8. 首选项 - 在Java 1.4中引入,允许您在特定于平台的存储中存储键入的文本,数字,字节数组和其他基元。在Windows上,这是注册表(您可以在HKLM或HKCU下选择/ Software / JavaSoft / Prefs)。在Unix下,相同的API在用户主目录或/ etc下创建文件。每个prefs配置单元都可以作为XML文件导出和导入。您可以通过将“java.util.prefs.PreferencesFactory”JVM属性设置为实现类名来指定PreferencesFactory接口的自定义实现。

In general using the prefs API can be a good or a bad thing based on your app scenario.

通常,根据您的应用场景,使用prefs API可能是好事还是坏事。

  1. If you plan to have multiple versions of the same code running on the same machine with different configuration, then using the Preferences API is a bad idea.
  2. 如果您计划在具有不同配置的同一台计算机上运行相同代码的多个版本,那么使用Preferences API是个坏主意。

  3. If you plan using the application in a restricted environment (Windows domain or tightly managed Unix box) you need to make sure that you have proper access to the necessary registry keys/directories. This has caught me by surprise more than once.
  4. 如果您计划在受限制的环境(Windows域或严格管理的Unix机箱)中使用该应用程序,则需要确保您具有对必要的注册表项/目录的正确访问权限。这不止一次让我感到意外。

  5. Beware from roaming profiles (replicated home dirs) they make up for some funny scenarios when more than one active machines are involved.
  6. 请注意漫游配置文件(复制的家庭目录),当涉及多个活动计算机时,它们会弥补一些有趣的情况。

  7. Preferences are not as obvious as a configuration file under the application's directory. most of the desktop support staff doesn't expect and doesn't like them.
  8. 首选项不如应用程序目录下的配置文件那么明显。大多数桌面支持人员并不期望也不喜欢他们。

Regarding the file layout of the prefs it again depends on your application. A generic suggestion is:

关于prefs的文件布局,它还取决于您的应用程序。一般建议是:

  1. Package most of your XML files inside application's JAR either in the root or under /META-INF directory. These files will be read-only and are considered private for the application.
  2. 将大多数XML文件打包在应用程序的JAR中,位于根目录或/ META-INF目录下。这些文件将是只读的,并被视为应用程序的私有文件。

  3. Put the user modifiable configuration under $APP_HOME/conf . It should consist mainly of properties files and occasionally a simple XML file (XStream serialization). These files are tweaked as part of the installation process and are usually not user serviceable.
  4. 将用户可修改的配置放在$ APP_HOME / conf下。它应该主要由属性文件组成,偶尔也应该包含一个简单的XML文件(XStream序列化)。这些文件作为安装过程的一部分进行了调整,通常不是用户可维护的。

  5. Under the user-home, in a dot-directory (i.e. '~/.myapplication') store any user configuration. The user configuration may override the one in the application conf directory. Any changes made from within the application go here (see also next point).
  6. 在user-home下,在点目录(即'〜/ .myapplication')中存储任何用户配置。用户配置可能会覆盖应用程序conf目录中的配置。从应用程序中进行的任何更改都会在此处进行(另请参阅下一点)。

  7. You can also use an $APP_HOME/var directory to store any other mutable data which is specific to this application instance (as opposed to the user). Another advantage of this approach is that you can move and backup the whole application and it's configuration by simple copy of one directory.
  8. 您还可以使用$ APP_HOME / var目录来存储特定于此应用程序实例的任何其他可变数据(而不是用户)。这种方法的另一个优点是,您可以通过简单复制一个目录来移动和备份整个应用程序及其配置。

This illustrates some standard techniques for managing configuration. You can implement them using different libraries and tools, starting from raw JRE, adding Spring/Guice or going for a full J2EE container (possibly with embedded Spring)

这说明了一些管理配置的标准技术。您可以使用不同的库和工具来实现它们,从原始JRE开始,添加Spring / Guice或者获取完整的J2EE容器(可能使用嵌入式Spring)

Other approaches for managing configuration are:

其他管理配置的方法是:

  1. Using multiple base directories for running multiple instances of the application using different configurations.
  2. 使用多个基目录使用不同的配置运行应用程序的多个实例。

  3. Using lightweight registries for centralized configuration management
  4. 使用轻量级注册表进行集中配置管理

  5. A centrally managed Configuration Management Database (CMDB) file, containing the host-specific values for each machine is rsync-ed every night to all production hosts. The application uses a templated configuration and selects from the CMDB during runtime based on the current hostname.
  6. 集中管理的配置管理数据库(CMDB)文件包含每台计算机的特定于主机的值,每晚都会对所有生产主机进行rsync。应用程序使用模板化配置,并在运行时根据当前主机名从CMDB中进行选择。

#3


5  

Java has a library specifically for doing this in java.util.prefs.Preferences.

Java有一个专门用于在java.util.prefs.Preferences中执行此操作的库。

Preferences userPrefs = Preferences.getUserNodeForPackage(MyClass.class); // Gets user preferences node for MyClass
Preferences systemPrefs = Preferences.getSysteNodeForPackage(MyClass.class); // Gets system preferences node for MyClass
Preferences userPrefsRoot = Preferences.getUserRoot(); // Gets user preferences root node
Preferences systemPrefsRoot = Preferences.getSystemRoot(); // Gets system preferences root node

#4


1  

You might want to look at Resource Bundles.

您可能想要查看资源包。

#5


0  

For user specific config, you could write a config file to the folder pointed to by the "user.home" system property. Would only work on that machine of course.

对于特定于用户的配置,您可以将配置文件写入“user.home”系统属性指向的文件夹。当然只适用于那台机器。

#6


0  

I use this

我用这个


    String pathFile = null;
    if(OS.contains("win")){
        pathFile = System.getenv("AppData");
    }else{
        pathFile = System.getProperty("user.home");
    }

I save the settings of my application here C:\Users\USERNAME\AppData\ on windows user.home (/home/USERNAME) on other platfroms

我将应用程序的设置保存在其他平台上的windows user.home(/ home / USERNAME)上的C:\ Users \ USERNAME \ AppData \

#1


13  

That depends on your kind of J2SE Application:

这取决于您的J2SE应用程序类型:

  • J2SE executable JAR file (very simple): use user.home System property to find home-dir. Then make a subdir accordingly (like e.g. PGP, SVN, ... do)
  • J2SE可执行JAR文件(很简单):使用user.home系统属性查找home-dir。然后相应地制作一个子目录(例如PGP,SVN,......)

  • Java Web Start provides very nice included methods to safe properties. Always user-specific
  • Java Web Start为安全属性提供了非常好的包含方法。始终是用户特定的

  • Finally Eclipse RCP: There you have the notion of the workspace (also derived from user.home) for users and configuration (not totally sure how to access that tricky in Vista) for computer wide usage
  • 最后Eclipse RCP:你有工作区的概念(也来自user.home)用于用户和配置(不完全确定如何在Vista中访问那些棘手的东西)以供计算机广泛使用

All these approaches are, when used with care -- use correct separatorChar -- OS neutral.

所有这些方法,在小心使用时 - 使用正确的separatorChar - OS中性。

#2


25  

First on the format:

首先是格式:

  1. Java property files are good for key/value pairs (also automatically handle the newline chars). A degree of structure is possible by using 'dot notation'. Drawback is that the structure does not allow you to easily enumerate top-level configuration entities and work in drill-down manner. Best used for a small set of often tweakable environment-specific settings
  2. Java属性文件适用于键/值对(也自动处理换行符)。使用“点符号”可以实现一定程度的结构。缺点是该结构不允许您轻松枚举*配置实体并以深入的方式工作。最适用于一小组经常可调整的环境特定设置

  3. XML files - quite often used for more complex configuration of various Java frameworks (notably J2EE and Spring). I would advice that you at least learn about Spring - it contains many ideas worth knowing even if you decide not to use it. If you decide to roll your own XML configuration, I'd recommend using XStream with customized serialization options or if you just need to parse some XML, take a look at XOM. BTW Spring also allows you to plug your custom XML configuration language, but it's a relatively complex task. XML configuration is best used for more complex 'internal' configuration that is not seen or tweaked by the end user.
  4. XML文件 - 经常用于各种Java框架(尤其是J2EE和Spring)的更复杂配置。我建议您至少要了解Spring - 它包含许多值得了解的想法,即使您决定不使用它。如果您决定推出自己的XML配置,我建议使用XStream和自定义序列化选项,或者如果您只需要解析一些XML,请查看XOM。 BTW Spring还允许您插入自定义XML配置语言,但这是一项相对复杂的任务。 XML配置最适用于最终用户未看到或调整的更复杂的“内部”配置。

  5. Serialized Java objects - a quick and easy way to persist the state of an object and restore it later. Useful if you write a configuration GUI and you don't care if the configuration is human readable. Beware of compatibility issues when you evolve classes.
  6. 序列化Java对象 - 一种快速简便的方法,可以持久保存对象的状态并在以后恢复。如果您编写配置GUI并且不关心配置是否可读,则非常有用。在进化类时要注意兼容性问题。

  7. Preferences - introduced in Java 1.4, allow you to store typed text, numbers, byte arrays and other primitives in platform-specific storage. On Windows, that is the registry (you can choose between /Software/JavaSoft/Prefs under HKLM or HKCU). Under Unix the same API creates files under the user home or /etc. Each prefs hive can be exported and imported as XML file. You can specify custom implementation of the PreferencesFactory interface by setting the "java.util.prefs.PreferencesFactory" JVM property to your implementation class name.
  8. 首选项 - 在Java 1.4中引入,允许您在特定于平台的存储中存储键入的文本,数字,字节数组和其他基元。在Windows上,这是注册表(您可以在HKLM或HKCU下选择/ Software / JavaSoft / Prefs)。在Unix下,相同的API在用户主目录或/ etc下创建文件。每个prefs配置单元都可以作为XML文件导出和导入。您可以通过将“java.util.prefs.PreferencesFactory”JVM属性设置为实现类名来指定PreferencesFactory接口的自定义实现。

In general using the prefs API can be a good or a bad thing based on your app scenario.

通常,根据您的应用场景,使用prefs API可能是好事还是坏事。

  1. If you plan to have multiple versions of the same code running on the same machine with different configuration, then using the Preferences API is a bad idea.
  2. 如果您计划在具有不同配置的同一台计算机上运行相同代码的多个版本,那么使用Preferences API是个坏主意。

  3. If you plan using the application in a restricted environment (Windows domain or tightly managed Unix box) you need to make sure that you have proper access to the necessary registry keys/directories. This has caught me by surprise more than once.
  4. 如果您计划在受限制的环境(Windows域或严格管理的Unix机箱)中使用该应用程序,则需要确保您具有对必要的注册表项/目录的正确访问权限。这不止一次让我感到意外。

  5. Beware from roaming profiles (replicated home dirs) they make up for some funny scenarios when more than one active machines are involved.
  6. 请注意漫游配置文件(复制的家庭目录),当涉及多个活动计算机时,它们会弥补一些有趣的情况。

  7. Preferences are not as obvious as a configuration file under the application's directory. most of the desktop support staff doesn't expect and doesn't like them.
  8. 首选项不如应用程序目录下的配置文件那么明显。大多数桌面支持人员并不期望也不喜欢他们。

Regarding the file layout of the prefs it again depends on your application. A generic suggestion is:

关于prefs的文件布局,它还取决于您的应用程序。一般建议是:

  1. Package most of your XML files inside application's JAR either in the root or under /META-INF directory. These files will be read-only and are considered private for the application.
  2. 将大多数XML文件打包在应用程序的JAR中,位于根目录或/ META-INF目录下。这些文件将是只读的,并被视为应用程序的私有文件。

  3. Put the user modifiable configuration under $APP_HOME/conf . It should consist mainly of properties files and occasionally a simple XML file (XStream serialization). These files are tweaked as part of the installation process and are usually not user serviceable.
  4. 将用户可修改的配置放在$ APP_HOME / conf下。它应该主要由属性文件组成,偶尔也应该包含一个简单的XML文件(XStream序列化)。这些文件作为安装过程的一部分进行了调整,通常不是用户可维护的。

  5. Under the user-home, in a dot-directory (i.e. '~/.myapplication') store any user configuration. The user configuration may override the one in the application conf directory. Any changes made from within the application go here (see also next point).
  6. 在user-home下,在点目录(即'〜/ .myapplication')中存储任何用户配置。用户配置可能会覆盖应用程序conf目录中的配置。从应用程序中进行的任何更改都会在此处进行(另请参阅下一点)。

  7. You can also use an $APP_HOME/var directory to store any other mutable data which is specific to this application instance (as opposed to the user). Another advantage of this approach is that you can move and backup the whole application and it's configuration by simple copy of one directory.
  8. 您还可以使用$ APP_HOME / var目录来存储特定于此应用程序实例的任何其他可变数据(而不是用户)。这种方法的另一个优点是,您可以通过简单复制一个目录来移动和备份整个应用程序及其配置。

This illustrates some standard techniques for managing configuration. You can implement them using different libraries and tools, starting from raw JRE, adding Spring/Guice or going for a full J2EE container (possibly with embedded Spring)

这说明了一些管理配置的标准技术。您可以使用不同的库和工具来实现它们,从原始JRE开始,添加Spring / Guice或者获取完整的J2EE容器(可能使用嵌入式Spring)

Other approaches for managing configuration are:

其他管理配置的方法是:

  1. Using multiple base directories for running multiple instances of the application using different configurations.
  2. 使用多个基目录使用不同的配置运行应用程序的多个实例。

  3. Using lightweight registries for centralized configuration management
  4. 使用轻量级注册表进行集中配置管理

  5. A centrally managed Configuration Management Database (CMDB) file, containing the host-specific values for each machine is rsync-ed every night to all production hosts. The application uses a templated configuration and selects from the CMDB during runtime based on the current hostname.
  6. 集中管理的配置管理数据库(CMDB)文件包含每台计算机的特定于主机的值,每晚都会对所有生产主机进行rsync。应用程序使用模板化配置,并在运行时根据当前主机名从CMDB中进行选择。

#3


5  

Java has a library specifically for doing this in java.util.prefs.Preferences.

Java有一个专门用于在java.util.prefs.Preferences中执行此操作的库。

Preferences userPrefs = Preferences.getUserNodeForPackage(MyClass.class); // Gets user preferences node for MyClass
Preferences systemPrefs = Preferences.getSysteNodeForPackage(MyClass.class); // Gets system preferences node for MyClass
Preferences userPrefsRoot = Preferences.getUserRoot(); // Gets user preferences root node
Preferences systemPrefsRoot = Preferences.getSystemRoot(); // Gets system preferences root node

#4


1  

You might want to look at Resource Bundles.

您可能想要查看资源包。

#5


0  

For user specific config, you could write a config file to the folder pointed to by the "user.home" system property. Would only work on that machine of course.

对于特定于用户的配置,您可以将配置文件写入“user.home”系统属性指向的文件夹。当然只适用于那台机器。

#6


0  

I use this

我用这个


    String pathFile = null;
    if(OS.contains("win")){
        pathFile = System.getenv("AppData");
    }else{
        pathFile = System.getProperty("user.home");
    }

I save the settings of my application here C:\Users\USERNAME\AppData\ on windows user.home (/home/USERNAME) on other platfroms

我将应用程序的设置保存在其他平台上的windows user.home(/ home / USERNAME)上的C:\ Users \ USERNAME \ AppData \