Tomcat 8 - context.xml use Environment Variable in Datasource

前端 未结 1 1869
庸人自扰
庸人自扰 2020-12-28 08:28

I have a Tomcat 8 project that uses a datasource (see below)



        
1条回答
  •  自闭症患者
    2020-12-28 09:21

    I finally discovered what I actually needed to do here.... Quite simple in the end.

    I passed in a java parameter to Tomcat at runtime as shown below.

    I added the following bits to setenv.sh

    export PUID=abcd
    
    JAVA_OPTS=-Dpuid=${PUID} 
    

    Then edited my context.xml as shown here

    
    

    So now my Tomcat installation will read this and be able to use a different path for each different PUID.


    Background: This works because Tomcat will automatically perform variable substition in its configuration files:

    Tomcat configuration files are formatted as schemaless XML; elements and attributes are case-sensitive.

    Apache Ant-style variable substitution is supported; a system property with the name propname may be used in a configuration file using the syntax ${propname}. All system properties are available including those set using the -D syntax, those automatically made available by the JVM and those configured in the $CATALINA_BASE/conf/catalina.properties file.

    Apache Tomcat 9 Configuration Reference - Overview

    The part:

    JAVA_OPTS=-Dpuid=${PUID}
    

    describe above is necessary because Tomcat will only read Java system properties (which are provided by the JVM), but not environment variables (which are provided by the OS/runtime libraries that the JVM is running on). The parameter -D sets a Java system property from the environment variable of the same name.

    0 讨论(0)
提交回复
热议问题