问题描述
我有一个使用数据源的Tomcat 8项目(见下文)
I have a Tomcat 8 project that uses a datasource (see below)
<Resource auth="Container"
name="jdbc/JtmDS"
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
type="javax.sql.DataSource"
username="xfer"
password="xfer10"
url="jdbc:derby:/home/PUID/tm/control/JtmDB"
initialSize="25"
maxTotal="100"
maxIdle="30"
maxWaitMillis="10000"
removeAbandonedOnBorrow="true"
removeAbandonedTimeout="20" />
这很好用.
然而,URL是硬编码的路径/home/PUID/tm/control/JtmDB
However the url is a hard-coded path /home/PUID/tm/control/JtmDB
当这付诸实践时,路径的PUID部分将在许多系统中有所不同.我有一个环境变量集export PUID=abcd
应用程序的其余部分能够在适当的地方使用System.getenv( )
或${env:PUID}
之类的东西.
When this gets into production the PUID part of the path will differ across numerous systems.I have an environment variable set export PUID=abcd
The rest of the application is able to use things like System.getenv( )
or ${env:PUID}
as and where appropriate.
这些都很好.
我的问题很简单:如何将context.xml中的PUID值设置为可以从环境变量读取的变量?
My question is very simply:How can I make the PUID value in my context.xml a variable that can be read from an environment variable?
推荐答案
我终于在这里发现了我真正需要做的....最后很简单.
I finally discovered what I actually needed to do here.... Quite simple in the end.
我在运行时将Java参数传递给Tomcat,如下所示.
I passed in a java parameter to Tomcat at runtime as shown below.
我在setenv.sh
export PUID=abcd
JAVA_OPTS=-Dpuid=${PUID}
然后按如下所示编辑我的context.xml
Then edited my context.xml as shown here
<Resource auth="Container"
name="jdbc/JtmDS"
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
type="javax.sql.DataSource"
username="xfer"
password="xfer10"
url="jdbc:derby:/home/${puid}/tm/control/JtmDB"
initialSize="25"
maxTotal="100"
maxIdle="30"
maxWaitMillis="10000"
removeAbandonedOnBorrow="true"
removeAbandonedTimeout="20" />
因此,现在我的Tomcat安装程序将读取此内容,并且能够为每个不同的PUID使用不同的路径.
So now my Tomcat installation will read this and be able to use a different path for each different PUID.
这篇关于Tomcat 8-context.xml在数据源中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!