2011-07-14 60 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); 
相關問題