2011-07-14 6 views
1

tomcatのconfフォルダにputtedされたファイルにアクセスできるかどうかを知りたいと思います。 通常、このファイルには、戦争の外にある複数のwebappの設定を行います。tomcatのconfフォルダにあるファイルにファイルへのアクセス

クラスパスをファイルシステムから独立させたいと思っています。

過去にlibフォルダを使用しました。それは素晴らしい仕事です。 しかし、libフォルダを使ってconfファイルを置くのはちょっと意味がありません。

誰か助けてもらえますか?

答えて

1

webappsで設定を変更するときに、実際に設定しない(設定を変更したときに再デプロイする必要がある)か、柔軟性がほとんどありません。

私はこの問題にどのようにアプローチするのですか?property placeholderの場合は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

プロパティは、システムプロパティは、このようにあなたが特定のWebアプリケーション用に上書きすることができ、その後、サーブレットコンテキストから最初に引かれます。あなたは、web.xmlを変更することにより、いずれかのcertian Webアプリケーションのための設定を変更することができます(推奨されません)またはcreating a context.xml

であなたは設定を取得するための静的メソッドを使用することができます :

public static SimpleContextListenerConfig getConfig(ServletContext sc); 
関連する問題