Externalize Tomcat configuration

后端 未结 4 1415
轻奢々
轻奢々 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:30

    It is easy with context deploy descriptors, which look like:

    
        
        
        
        
    
        
        
        
    
    

    There are several places where you can put this config, for my opinion best option is $CATALINA_BASE/conf/[enginename]/[hostname]/$APP.xml

    In above XML Context can hold custom Loader org.apache.catalina.loader.VirtualWebappLoader (available in modern Tomcat 7, you can add own separate classpath per application to your .properties files), Parameter (accessed via FilterConfig.getServletContext().getInitParameter(name)) and Environment (accessed via new InitialContext().lookup("java:comp/env").lookup("name"))

    See discussion at:

    • Adding a directory to tomcat classpath
    • Can I create a custom classpath on a per application basis in Tomcat
    • How to read a properties file outside my webapp context in Tomcat
    • Configure Tomcat to use properties file to load DB connection information
    • Should you set up database connection properties in server.xml or context.xml

    UPDATE Tomcat 8 change syntax for and elements, corresponding part now look like:

    
        
        
    
    

提交回复
热议问题