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

查看:110
本文介绍了在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.

将所有内容存储在数据库中会很好,但显然你不能在数据库中存储数据库连接信息。

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).

唯一我能想到的其他解决办法,除了DB登录信息之外,还能保留数据库中的所有内容。这将来自通过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天全站免登陆