配置Tomcat以使用属性文件来加载数据库连接信息

时间:2021-12-09 03:13:31

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文件并指定客户端只需要在特定目录中创建或编辑属性文件,这将是一件好事。这是一种有点常规的做事方式吗?有比这更好的方法吗?

3 个解决方案

#1


14  

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文件来分发webapps,该文件放在tomcat / conf / Catalina / localhost目录中,并且可以从任何路径加载webapp。这里有一个参考文件。这提供了以下优点:

  • Context parameters can be configured here and read by the webapp
  • 可以在此处配置上下文参数并由webapp读取

  • DataSources can be defined and configured here
  • 可以在此处定义和配置DataSource

  • 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
  • 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 ServletContextListener and then read in the properties file.

如果您确实需要属性文件,可以在指向属性文件的上下文XML文件中设置参数,在ServletContextListener中读取参数,然后读入属性文件。

#2


9  

The way we handle this:

我们处理这个的方式:

  1. Have the client create a connection pool in GlobalNamingResources using a resource name we agree on. The database driver needs to be in Tomcat's classpath.
  2. 让客户端使用我们同意的资源名称在GlobalNamingResources中创建连接池。数据库驱动程序需要位于Tomcat的类路径中。

  3. Our war file includes a META-INF/context.xml files that has a ResourceLink linking to the connection pool configured in step 1.
  4. 我们的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>

#3


8  

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>

#1


14  

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文件来分发webapps,该文件放在tomcat / conf / Catalina / localhost目录中,并且可以从任何路径加载webapp。这里有一个参考文件。这提供了以下优点:

  • Context parameters can be configured here and read by the webapp
  • 可以在此处配置上下文参数并由webapp读取

  • DataSources can be defined and configured here
  • 可以在此处定义和配置DataSource

  • 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
  • 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 ServletContextListener and then read in the properties file.

如果您确实需要属性文件,可以在指向属性文件的上下文XML文件中设置参数,在ServletContextListener中读取参数,然后读入属性文件。

#2


9  

The way we handle this:

我们处理这个的方式:

  1. Have the client create a connection pool in GlobalNamingResources using a resource name we agree on. The database driver needs to be in Tomcat's classpath.
  2. 让客户端使用我们同意的资源名称在GlobalNamingResources中创建连接池。数据库驱动程序需要位于Tomcat的类路径中。

  3. Our war file includes a META-INF/context.xml files that has a ResourceLink linking to the connection pool configured in step 1.
  4. 我们的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>

#3


8  

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>