配置Tomcat使用properties文件加载DB连接信息 [英] Configure Tomcat to use properties file to load DB connection information
问题描述
创建从属性文件读取配置参数的 Tomcat 部署的公认做法是什么?
What are the accepted practices for creating a Tomcat deployment that reads configuration parameters from a properties file?
如果能够提供 WAR 文件并指定客户端只需要在特定目录中创建或编辑属性文件,那就太好了.这是一种有点常规的做事方式吗?还有比这更好的方法吗?
It would be nice to be able to deliver a WAR file and specify that the client need only create or edit a properties file in a specific directory. Is this a somewhat regular way of doing things? Is there a better approach than this?
推荐答案
我们经常通过提供一个 WAR 和一个上下文 XML 文件来分发 web 应用程序,该文件被放置在您的 tomcat/conf/Catalina/localhost
目录,并且可以从任何路径加载 webapp.有一个参考文档这里.这提供了以下优点:
We often distribute webapps by providing a WAR, and a Context XML file, which gets placed into your tomcat/conf/Catalina/localhost
directory, and can load the webapp from any path. There is a reference document here. This provides the following advantages:
- 可以在此处配置上下文参数并由 web 应用程序读取
- 可以在此处定义和配置数据源
- WAR 实际上可以存在于文件系统的任何位置,这意味着如果 Tomcat 升级,只需将这个单个配置文件移动到新的 Tomcat 安装中,Web 应用程序和任何其他文件就可以保持原样 莉>
如果你真的想要一个属性文件,你可以在指向你的属性文件的上下文 XML 文件中设置一个参数,在 ServletContextListener 然后读入属性文件.
If you really want a properties file, you can set a parameter in the context XML file pointing to your properties file, read the parameter in a ServletContextListener and then read in the properties file.
这篇关于配置Tomcat使用properties文件加载DB连接信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!