在 Java Web 应用程序 (WAR) 中存储配置文件的最佳位置是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/96247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best place to store a configuration file in a Java web application (WAR)?
提问by boes
I create a web application (WAR) and deploy it on Tomcat. In the webappthere 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 应用程序 (WAR) 并将其部署在 Tomcat 上。在webapp 中有一个带有表单的页面,管理员可以在其中输入一些配置数据。我不想将此数据存储在 DBMS 中,而只想存储在文件系统上的 XML 文件中。把它放在哪里?
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-INFdirectory? Or put it somewhere else?
我想将该文件放在部署应用程序本身的目录树中的某个位置。我的配置文件应该在WEB-INF目录中吗?还是放在别的地方?
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?
在 servlet 中使用什么 Java 代码来查找目录的绝对路径?还是可以通过相对路径访问?
采纳答案by MBCook
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.
我们所做的是将它放在服务器上的一个单独目录中(您可以使用 /config、/opt/config、/root/config、/home/username/config 或任何您想要的内容)。当我们的 servlet 启动时,它们读取 XML 文件,从中获取一些信息(最重要的是 DB 连接信息),仅此而已。
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.
将所有内容存储在 DB 中会很好,但显然您不能将 DB 连接信息存储在 DB 中。
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 文件的副本,他们就会获得该信息。
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 文件中看起来不错,但如果你想改变很多东西,这可能是一个坏主意。问题是,如果您必须更改信息,那么下次重新部署时,它会覆盖该文件,因此您不记得在内置于 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).
文件系统上特殊位置的文件对我们来说效果很好。它没有任何大的缺点。您知道它在哪里,它是单独存储的,如果它们都需要不同的配置值(因为它不是 WAR 的一部分),则可以轻松部署到多台机器。
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.
我能想到的唯一其他解决方案是将除数据库登录信息外的所有内容都保留在数据库中。这将来自通过 JVM 检索的 Java 系统属性。这是上面 Hans Doggen 提到的 Preferences API 东西。我不认为它在我们的应用程序第一次开发时就已经存在了,如果它没有被使用的话。
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.
至于访问配置文件的路径,它只是文件系统上的一个文件。您无需担心 Web 路径。因此,当您的 servlet 启动时,它只需打开位于“/config/myapp/config.xml”(或其他位置)的文件,它就会找到正确的内容。只是硬编码这个路径对我来说似乎无害。
回答by Hans Doggen
I would not store it in the application folder, because that would override the configuration with a new deployment of the application.
我不会将它存储在应用程序文件夹中,因为这会使用应用程序的新部署覆盖配置。
I suggest you have a look at the Preferences API, or write something in the users folder (the user that is running Tomcat).
我建议你看看 Preferences API,或者在 users 文件夹(运行 Tomcat 的用户)中写一些东西。
回答by delfuego
The answer to this depends on how you intend to read and write that config file.
答案取决于您打算如何读取和写入该配置文件。
For example, the Spring framework gives you the ability to use XML configuration files(or Java property files); these can be stored in your classpath (e.g., in the WEB-INF directory), anywhere else on the filesystem, or even in memory. If you were to use Spring for this, then the easiest place to store the config file is in your WEB-INF directory, and then use Spring's ClassPathXmlApplicationContextclass to access your configuration file.
例如,Spring 框架使您能够使用 XML 配置文件(或 Java 属性文件);这些可以存储在您的类路径中(例如,在 WEB-INF 目录中)、文件系统上的任何其他位置,甚至在内存中。如果您为此使用 Spring,那么存储配置文件最简单的位置是在您的 WEB-INF 目录中,然后使用 Spring 的ClassPathXmlApplicationContext类来访问您的配置文件。
But again, it all depends on how you plan to access that file.
但同样,这完全取决于您计划如何访问该文件。
回答by axk
If it is your custom config WEB-INF is a good place for it. But some libraries may require configs to reside in WEB-INF/classes.
如果它是您的自定义配置 WEB-INF 是一个好地方。但是一些库可能需要配置驻留在 WEB-INF/classes 中。
回答by Jataro
WEB-INF is a good place to put your config file. Here's some code to get the absolute path of the directory from a servlet.
WEB-INF 是放置配置文件的好地方。下面是一些从 servlet 获取目录绝对路径的代码。
public void init(ServletConfig servletConfig) throws ServletException{
super.init(servletConfig);
String path = servletConfig.getServletContext().getRealPath("/WEB-INF")
回答by Michael
Putting it in WEB-INF
will hide the XML file from users who try to access it directly through a URL, so yes, I'd say put it in WEB-INF
.
将它放入WEB-INF
会隐藏 XML 文件,用户试图通过 URL 直接访问它,所以是的,我会说将它放入WEB-INF
.