Java 配置Tomcat使用properties文件加载DB连接信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1380793/
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
Configure Tomcat to use properties file to load DB connection information
提问by jW.
What are the accepted practices for creating a Tomcat deployment that reads configuration parameters from a properties file?
创建从属性文件读取配置参数的 Tomcat 部署的公认做法是什么?
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 文件并指定客户端只需要在特定目录中创建或编辑属性文件,那就太好了。这是一种有点常规的做事方式吗?还有比这更好的方法吗?
采纳答案by Adam Batkin
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:
我们经常通过提供 WAR 和 Context XML 文件来分发tomcat/conf/Catalina/localhost
webapp ,该文件放置在您的目录中,并且可以从任何路径加载 webapp。有一个参考文件在这里。这提供了以下优点:
- Context parameters can be configured here and read by the webapp
- DataSources can be defined and configured here
- The WAR can actually live anywhere on the filesystem, which means that if Tomcat gets upgraded, only this single configuration file needs to be moved to the new Tomcat install, the web application and any other files can stay where they are
- 可以在此处配置上下文参数并由 webapp 读取
- 可以在这里定义和配置数据源
- WAR 实际上可以存在于文件系统的任何位置,这意味着如果 Tomcat 升级,只需要将这个单个配置文件移动到新的 Tomcat 安装中,Web 应用程序和任何其他文件就可以保持原样
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 ServletContextListenerand then read in the properties file.
如果你真的想要一个属性文件,你可以在指向你的属性文件的上下文 XML 文件中设置一个参数,在ServletContextListener 中读取参数,然后在属性文件中读取。
回答by Steve K
The way we handle this:
我们处理这个的方式:
- Have the client create a connection pool in GlobalNamingResourcesusing a resource name we agree on. The database driver needs to be in Tomcat's classpath.
- Our war file includes a META-INF/context.xml files that has a ResourceLinklinking to the connection pool configured in step 1.
- 让客户端使用我们同意的资源名称在GlobalNamingResources 中创建一个连接池。数据库驱动程序需要在 Tomcat 的类路径中。
- 我们的 war 文件包含一个 META-INF/context.xml 文件,该文件具有链接到步骤 1 中配置的连接池的ResourceLink。
This is a little more up front work than simply altering the context.xml connection information directly, but over time it should pay off. A development server would be setup with it's GlobalNamingResources pointing to development, and a test server point to test etc. Then, the same WAR file can be copied to each server without editing anything.
这比简单地直接更改 context.xml 连接信息要多一些前期工作,但随着时间的推移它应该会有所回报。将设置一个开发服务器,其 GlobalNamingResources 指向开发,测试服务器指向测试等。然后,可以将相同的 WAR 文件复制到每个服务器,而无需编辑任何内容。
This isn't using properties files, but I think it achieves the same goal. Allowing a user/customer to setup the database connection information.
这不是使用属性文件,但我认为它实现了相同的目标。允许用户/客户设置数据库连接信息。
Example of GlobalNamingResource:
GlobalNamingResource 示例:
<Resource name="jdbc/dbconnection" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1546:SID"
username="scott" password="tiger" maxActive="8" maxIdle="4"
validationQuery="select 1 from dual"
testOnBorrow="true"/>
Example of context.xml in war file:
war 文件中的 context.xml 示例:
<Context path="/MyWebApp" docBase="MyWebApp" debug="5" reloadable="true">
<ResourceLink name="jdbc/dbconnection" global="jdbc/dbconnection"
type="javax.sql.DataSource"/>
</Context>
回答by ZZ Coder
It's a good practice to store configuration out of the war zone. In our WAR, we have a default location to look for the property file. If the default doesn't work, you can specify the location via a JVM parameter or a context parameter defined in context fragment in conf/Catalina/[host] directory. For example,
将配置存储在战区之外是一种很好的做法。在我们的 WAR 中,我们有一个默认位置来查找属性文件。如果默认设置不起作用,您可以通过 JVM 参数或在 conf/Catalina/[host] 目录中的上下文片段中定义的上下文参数指定位置。例如,
<Context docBase="/server/app.war"
swallowOutput="true" unpackWAR="false" useNaming="false">
<Parameter name="config-file" value="/config/db.properties" override="true" />
</Context>