Externalize Tomcat configuration

后端 未结 4 1425
轻奢々
轻奢々 2020-12-06 10:20

I have a DataSource configuration in context.xml. Is it possible not to hard-code database parameters in that file? For example, use an external properties file, and load th

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 10:50

    As stated here, you could do this in the following way.

    1.Download tomcat library to get the interface definition, for instance by defining maven dependency:

        
            org.apache.tomcat
            tomcat-coyote
            7.0.47
        
    

    2.Next step is to create a com.mycompany.MyPropertyDecoder in the following way:

    import org.apache.tomcat.util.IntrospectionUtils;
    public class MyPropertyDecoder implements IntrospectionUtils.PropertySource  {
        @Override
        public String getProperty(String arg0) {
            //TODO read properties here
            return null;
        }
    }
    

    3.Put MyPropertyDecoder.class into tomcat7/lib folder
    4.Define org.apache.tomcat.util.digester. PROPERTY_SOURCE property at tomcat7/conf/catalina.properties as following:

    org.apache.tomcat.util.digester.PROPERTY_SOURCE=com.mycompany.MyPropertyDecoder
    

    5.Update your context.xml with properties vars

    6.Put application.properties file somewhere in your project/container
    7.Make sure MyPropertyDecoder correctly reads application.properties
    8.Enjoy!

    PS Also there is a similar approach described for tc Server.

提交回复
热议问题