在 Web 应用程序中保存设置文件
对于如何在 J2EE 项目中保留应用程序级别设置/配置文件,是否有任何建议?我有一个 Web 项目,它具有必须写入日志的依赖项和一个本地设置文件。无论如何,是否可以将设置文件存储在本地目录中并使其可写?
[一些依赖项是:hibernate 和 gridgain,因此它们会生成大量日志]
我宁愿不对设置文件的位置进行硬编码。
Are there any recommendations on how to keep an application level settings/configuration file in a J2EE project? I have a web project that has dependences that must write to the logs, and a local settings file. Is there anyway to store the settings file in the local directory and have it writable?
[Some of the dependencies are: hibernate and gridgain, so they produce a lot of logs]
I would rather not hard code the location of the settings file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该将配置文件放在
WEB-INF
文件夹下。您可以创建一个目录/WEB-INF/config
。如果它们应该位于类路径中,请将它们放入/WEB-INF/classes
中。通过这种方式,该位置不是硬编码的,而是位于应用程序众所周知的位置。将它们放入
/WEB-INF
中非常重要,因此浏览器无法“访问”这些文件。另一种方法可能是向
web.xml
配置添加一个参数,您可以在其中指定配置文件的位置,以便它们不在 webapps 目录内。您可以通过添加context-param
来做到这一点。例如:该值将作为名为 configuration 的 servlet 上下文初始化参数对您的 Web 应用程序可见。然后,您可以将其用作读取配置文件的基本路径(通过从
ServletContext
调用getInitParameter
方法获取值)。但这仅适用于配置文件,而类路径中不需要这些文件。
You should put the configuration files under the
WEB-INF
folder. You could create a directory/WEB-INF/config
. If they should be in the classpath put them to/WEB-INF/classes
. By doing it this way, the location is not hardcoded, but at a well known place for your application.Putting them inside
/WEB-INF
is important, so the files are not "accessible" by the browser.Another way could be to add a parameter to the
web.xml
configuration, where you specify the location of your configuration files so they are not inside the webapps directory. You could do this by adding acontext-param
. For example:The value will be made visible to your web application as a servlet context initialization parameter named configuration. You can then use it as a base path to read your configuration files (Get the value by calling the
getInitParameter
method from theServletContext
).But this will only work for config files, which are not needed in the classpath.
只需将配置文件的路径添加到 Web 应用程序的类路径即可。这是常见的方法,许多 API 也被设计为从类路径加载配置。对于手动加载这些文件的情况,您可以使用
ClassLoader#getResourceAsStream()
获取类路径资源的InputStream
。首先,在 webapp 目录之外的某个位置创建一个固定路径来放置配置文件,例如
/var/webapp/config
。然后配置 servlet 容器以在类路径中包含此路径。对于 Tomcat,您可以在/conf/catalina.properties
文件的shared.loader
或common.loader
属性中指定它。Just add the path to the configuration files to the webapp's classpath. That's the common approach and many API's are also designed to load the configuration from the classpath. For the case you're loading those files manually, you can use
ClassLoader#getResourceAsStream()
to get anInputStream
of the classpath resource.First, create a fixed path somewhere outside the webapp directory to drop the configurationfiles in, e.g.
/var/webapp/config
. Then configure the servletcontainer to include this path in the classpath. In case of Tomcat, you can specify it in theshared.loader
orcommon.loader
property of the/conf/catalina.properties
file.同意 Soundlink 的观点。
您还可以将您的设置存储在数据库中。当您的 servlet 启动时,应从数据库加载设置数据。
非 Java EE 的配置文件
标准。
向上。
Agreed with Soundlink.
You also could store your setting in database. When your servlet starts up, the setting data should be loaded from database.
configuration files which are not Java EE
standard.
up.
由于应用程序需要支持集群,因此将设置保留在数据库中会更好
Keeping settings in database is better as the application needs to support clustering