简体   繁体   English

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

[英]What is the proper way to store app's conf data in Java?

Where do you store user-specific and machine-specific runtime configuration data for J2SE application?您将 J2SE 应用程序的特定用户和特定于机器的运行时配置数据存储在何处?

(For example, C:\\Users\\USERNAME\\AppData\\Roaming</em> on Windows and /home/username on Unix) (例如, Windows 上的 C:\\Users\\USERNAME\\AppData\\Roaming</em> 和Unix 上的/home/username

How do you get these locations in the filesystem in platform-independent way?您如何以独立于平台的方式在文件系统中获取这些位置?

First on the format:先说格式:

  1. Java property files are good for key/value pairs (also automatically handle the newline chars). Java 属性文件适用于键/值对(也自动处理换行符)。 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. XML files - quite often used for more complex configuration of various Java frameworks (notably J2EE and Spring). XML 文件 - 经常用于各种 Java 框架(特别是 J2EE 和 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.我建议您至少了解Spring - 即使您决定不使用它,它也包含许多值得了解的想法。 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 .如果您决定推出自己的 XML 配置,我建议您使用带有自定义序列化选项的XStream ,或者如果您只需要解析一些 XML,请查看XOM BTW Spring also allows you to plug your custom XML configuration language, but it's a relatively complex task .顺便说一句,Spring 还允许您插入自定义 XML 配置语言,但这是一项相对复杂的任务 XML configuration is best used for more complex 'internal' configuration that is not seen or tweaked by the end user. XML 配置最适合用于最终用户无法看到或调整的更复杂的“内部”配置。
  3. Serialized Java objects - a quick and easy way to persist the state of an object and restore it later.序列化 Java 对象 - 一种保持对象状态并稍后恢复的快速简便的方法。 Useful if you write a configuration GUI and you don't care if the configuration is human readable.如果您编写配置 GUI 并且您不关心配置是否是人类可读的,则很有用。 Beware of compatibility issues when you evolve classes.进化类时要注意兼容性问题
  4. Preferences - introduced in Java 1.4 , allow you to store typed text, numbers, byte arrays and other primitives in platform-specific storage.首选项 - 在Java 1.4 中引入,允许您在特定于平台的存储中存储键入的文本、数字、字节数组和其他原语。 On Windows, that is the registry (you can choose between /Software/JavaSoft/Prefs under HKLM or HKCU ).在 Windows 上,这是注册表(您可以在HKLMHKCU下的/Software/JavaSoft/Prefs之间进行选择)。 Under Unix the same API creates files under the user home or /etc .在 Unix 下,相同的 API 在用户 home 或/etc下创建文件。 Each prefs hive can be exported and imported as XML file.每个偏好配置单元都可以作为 XML 文件导出和导入。 You can specify custom implementation of the PreferencesFactory interface by setting the "java.util.prefs.PreferencesFactory" JVM property to your implementation class name.您可以通过将“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.如果您计划在具有不同配置的同一台机器上运行相同代码的多个版本,那么使用 Preferences API 是一个坏主意。
  2. 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.如果您计划在受限环境(Windows 域或严格管理的 Unix 机器)中使用该应用程序,您需要确保您可以正确访问必要的注册表项/目录。 This has caught me by surprise more than once.这让我不止一次感到意外。
  3. Beware from roaming profiles (replicated home dirs) they make up for some funny scenarios when more than one active machines are involved.当心漫游配置文件(复制的家庭目录),当涉及多台活动机器时,它们弥补了一些有趣的场景。
  4. 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.大多数桌面支持人员不期望也不喜欢它们。

Regarding the file layout of the prefs it again depends on your application.关于首选项的文件布局,它再次取决于您的应用程序。 A generic suggestion is:一个通用的建议是:

  1. Package most of your XML files inside application's JAR either in the root or under /META-INF directory.将大部分 XML 文件打包在应用程序的 JAR 中,位于根目录或 /META-INF 目录下。 These files will be read-only and are considered private for the application.这些文件将是只读的,并被视为应用程序的私有文件。
  2. Put the user modifiable configuration under $APP_HOME/conf .将用户可修改的配置放在$APP_HOME/conf 下 It should consist mainly of properties files and occasionally a simple XML file (XStream serialization).它应该主要由属性文件组成,偶尔还有一个简单的 XML 文件(XStream 序列化)。 These files are tweaked as part of the installation process and are usually not user serviceable.这些文件作为安装过程的一部分进行了调整,通常用户无法使用。
  3. Under the user-home, in a dot-directory (ie '~/.myapplication') store any user configuration.在 user-home 下,在一个点目录(即“~/.myapplication”)中存储任何用户配置。 The user configuration may override the one in the application conf directory.用户配置可能会覆盖应用程序conf目录中的配置 Any changes made from within the application go here (see also next point).在应用程序内进行的任何更改都在此处(另请参见下一点)。
  4. 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).您还可以使用$APP_HOME/var目录来存储特定于该应用程序实例(而不是用户)的任何其他可变数据。 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.这种方法的另一个优点是您可以通过简单复制一个目录来移动和备份整个应用程序及其配置。

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. Using lightweight registries for centralized configuration management使用轻量级注册表进行集中配置管理
  3. 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.一个集中管理的配置管理数据库 (CMDB) 文件,包含每台机器的主机特定值,每晚都会同步到所有生产主机。 The application uses a templated configuration and selects from the CMDB during runtime based on the current hostname.应用程序使用模板化配置,并在运行时根据当前主机名从 CMDB 中进行选择。

That depends on your kind of J2SE Application:这取决于您的 J2SE 应用程序类型:

  • J2SE executable JAR file (very simple): use user.home System property to find home-dir. J2SE 可执行 JAR 文件(非常简单):使用user.home 系统属性查找 home-dir。 Then make a subdir accordingly (like eg PGP, SVN, ... do)然后相应地创建一个子目录(例如 PGP、SVN 等)
  • Java Web Start provides very nice included methods to safe properties. Java Web Start 提供了非常好的方法来保护属性。 Always user-specific始终特定于用户
  • 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:在那里你有用户和配置(不完全确定如何在 Vista 中访问那个棘手的)的工作区(也派生自 user.home)的概念,以供计算机广泛使用

All these approaches are, when used with care -- use correct separatorChar -- OS neutral.所有这些方法在小心使用时 - 使用正确的separatorChar - 操作系统中立。

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

您可能想查看Resource Bundles

For user specific config, you could write a config file to the folder pointed to by the "user.home" system property.对于用户特定的配置,您可以将配置文件写入“user.home”系统属性指向的文件夹。 Would only work on that machine of course.当然只能在那台机器上工作。

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\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google App Engine中使用Java,什么是存储和访问大型静态数据的最佳方法? - Using Java in Google App Engine, what's the best way to store and access large, static data? 将数据从 java 程序存储到 MySQL 的最简单方法是什么? - What's the simplest way to store data from a java program to MySQL? Java:更新TableModel数据的正确方法? - Java: Proper way to update TableModel's data? 在Java中声明项目常量的正确方法是什么? - What's the proper way of declaring project constants in Java? Java Swing-做基于阶段的GUI的正确方法是什么? - Java Swing - What's the proper way of doing a stage based GUI? 在Java的UI中显示资金的正确方法是什么? - What's the proper way to show money in an UI in Java? 用Java编写自己的事件的正确方法是什么 - What's proper way to make my own events in java 将操作存储为java中的数据的最佳方法是什么? - What is the best way to store actions as data in java? 格式化.txt文件中数据的正确方法是什么? (JAVA) - What is the proper way to format data in a .txt file? (Java) 在Java中更改继承的成员变量的数据类型的正确方法是什么? - What is the Proper way to Change the Data Type of an Inherited Member Variable in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM