在 Java Web 应用程序 (WAR) 中存储配置文件的最佳位置是什么? [英] What is the best place to store a configuration file in a Java web application (WAR)?

查看:16
本文介绍了在 Java Web 应用程序 (WAR) 中存储配置文件的最佳位置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Web 应用程序 (WAR) 并将其部署在 Tomcat 上.在 webapp 中有一个页面,其中有一个表单,管理员可以在其中输入一些配置数据.我不想将此数据存储在 DBMS 中,而只想存储在文件系统上的 XML 文件中.把它放在哪里?

I create a web application (WAR) and deploy it on Tomcat. In the webapp there is a page with a form where an administrator can enter some configuration data. I don't want to store this data in an DBMS, but just in an XML file on the file system. Where to put it?

我想将该文件放在部署应用程序本身的目录树中的某个位置.我的配置文件应该在 WEB-INF 目录中吗?还是放在别的地方?

I would like to put the file somewhere in the directory tree where the application itself is deployed. Should my configuration file be in the WEB-INF directory? Or put it somewhere else?

在 servlet 中用于查找目录绝对路径的 Java 代码是什么?还是可以通过相对路径访问?

And what is the Java code to use in a servlet to find the absolute path of the directory? Or can it be accessed with a relative path?

推荐答案

我们所做的是将它放在服务器上的一个单独目录中(您可以使用类似/config、/opt/config、/root/config、/home/username/config,或任何你想要的).当我们的 servlet 启动时,它们会读取 XML 文件,从中获取一些信息(最重要的是数据库连接信息),仅此而已.

What we do is to put it in a separate directory on the server (you could use something like /config, /opt/config, /root/config, /home/username/config, or anything you want). When our servlets start up, they read the XML file, get a few things out of it (most importantly DB connection information), and that's it.

我问过我们为什么要这样做一次.

I asked about why we did this once.

将所有内容都存储在 DB 中会很好,但显然您不能将 DB 连接信息存储在 DB 中.

It would be nice to store everything in the DB, but obviously you can't store DB connection information in the DB.

您可以在代码中进行硬编码,但由于多种原因,这很丑陋.如果信息必须更改,您必须重建代码并重新部署.如果有人获得了您的代码或 WAR 文件的副本,他们就会获得该信息.

You could hardcode things in the code, but that's ugly for many reasons. If the info ever has to change you have to rebuild the code and redeploy. If someone gets a copy of your code or your WAR file they would then get that information.

把东西放在 WAR 文件中看起来不错,但如果你想改变很多东西,这可能是个坏主意.问题是,如果您必须更改信息,那么下次重新部署时,它会覆盖该文件,因此您在构建到 WAR 中的版本中不记得更改的任何内容都会被遗忘.

Putting things in the WAR file seems nice, but if you want to change things much it could be a bad idea. The problem is that if you have to change the information, then next time you redeploy it will overwrite the file so anything you didn't remember to change in the version getting built into the WAR gets forgotten.

文件系统上特殊位置的文件对我们来说效果很好.它没有任何大的缺点.您知道它在哪里,它单独存储,如果它们都需要不同的配置值,则可以轻松部署到多台机器(因为它不是 WAR 的一部分).

The file in a special place on the file system thing works quite well for us. It doesn't have any big downsides. You know where it is, it's stored seperatly, makes deploying to multiple machines easy if they all need different config values (since it's not part of the WAR).

我能想到的唯一其他可行的解决方案是将所有内容保留在数据库中,但数据库登录信息除外.这将来自通过 JVM 检索的 Java 系统属性.这是上面 Hans Doggen 提到的 Preferences API 东西.我不认为它在我们的应用程序最初开发时就已经存在了,如果它没有被使用的话.

The only other solution I can think of that would work well would be keeping everything in the DB except the DB login info. That would come from Java system properties that are retrieved through the JVM. This the Preferences API thing mentioned by Hans Doggen above. I don't think it was around when our application was first developed, if it was it wasn't used.

至于访问配置文件的路径,它只是文件系统上的一个文件.您无需担心 Web 路径.因此,当您的 servlet 启动时,它只需打开位于/config/myapp/config.xml"(或其他位置)的文件,它就会找到正确的内容.只是为这个路径硬编码对我来说似乎无害.

As for the path for accessing the configuration file, it's just a file on the filesystem. You don't need to worry about the web path. So when your servlet starts up it just opens the file at "/config/myapp/config.xml" (or whatever) and it will find the right thing. Just hardcodeing the path in for this one seems pretty harmless to me.

这篇关于在 Java Web 应用程序 (WAR) 中存储配置文件的最佳位置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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