2011-07-14 38 views
1

我想知道是否有可能访问放在tomcat的conf文件夹中的文件。 通常我会把这个文件中的多个webapp的配置放在战争之外。访问文件到文件在tomcat的conf文件夹中的文件

我想使用类路径独立于文件系统。

我在过去使用了lib文件夹。它工作得很好。 但是使用lib文件夹来放置conf文件是毫无意义的。

有人可以帮我这个吗?

回答

1

我已经看到很多不好的方法,人们在webapps中进行配置,要么不是真正的配置(当你改变配置时你必须重新部署/发布),或者你几乎没有灵活性。

我如何解决这个问题是使用Spring的property placeholder,但通常情况下,您需要引导Spring或任何你MVC的堆栈之前,它加载一个属性,说什么加载配置。我用一个监听器为:

package com.evocatus.util; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 


public class SimpleContextListenerConfig /*extend ResourceBundle */ implements ServletContextListener{ 

    private ServletContext servletContext; 

    @Override 
    public void contextInitialized(ServletContextEvent sce) { 
     servletContext = sce.getServletContext(); 
     servletContext.setAttribute(getClass().getCanonicalName(), this); 
    } 

    @Override 
    public void contextDestroyed(ServletContextEvent sce) { 

    } 

    public static String getProperty(ServletContext sc, String propName, String defaultValue) { 
     SimpleContextListenerConfig config = getConfig(sc); 
     return config.getProperty(propName, defaultValue); 
    } 

    public static SimpleContextListenerConfig getConfig(ServletContext sc) { 
     SimpleContextListenerConfig config = 
      (SimpleContextListenerConfig) sc.getAttribute(SimpleContextListenerConfig.class.getCanonicalName()); 
     return config; 
    } 

    public String getProperty(String propName, String defaultValue) 
    { 
     /* 
     * TODO cache properties 
     */ 
     String property = null; 

     if (property == null) 
      property = servletContext.getInitParameter(propName); 
     if (property == null) 
      System.getProperty(propName, null); 
     //TODO Get From resource bundle 
     if (property == null) 
      property = defaultValue; 

     return property; 
    } 
} 

https://gist.github.com/1083089

的属性将首先从servlet上下文拉,然后系统属性从而允许您覆盖某些web应用。 您可以通过改变web.xml中改变certian Web应用程序的配置(不推荐)或creating a context.xml

您可以使用静态方法来获取配置:

public static SimpleContextListenerConfig getConfig(ServletContext sc);