访问文件到文件 tomcat 的 conf 文件夹中的文件



我想知道是否有可能访问tomcat conf文件夹上的文件。通常情况下,我会把多个web应用的配置,除了战争,在这个文件中。

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

我过去使用lib文件夹。效果很好。但是使用lib文件夹来存放conf文件有点没有意义。

有人能帮我解决这个问题吗?

我在webapps中看到很多糟糕的配置方式,要么使它不是真正的配置(当你改变配置时,你必须做重新部署/发布),要么你的灵活性很小。

我如何处理这个问题是使用Spring的属性占位符,但通常情况下,你需要引导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(不推荐)或创建context.xml

来更改某些web应用程序的配置。

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

public static SimpleContextListenerConfig getConfig(ServletContext sc);

最新更新