2

WEB-INFディレクトリの下に、 configDEV.propertiesおよびconfigPRO.propertiesという重複した構成ファイルがあります(1つは開発環境用、もう1つは実稼働環境用)。

これらのSpring宣言とこのTomcat起動パラメーターのおかげで、適切なファイルをロードします。

<context:property-placeholder 
        location="WEB-INF/config${project.environment}.properties" />

-Dproject.environment=PRO
(or –Dproject.environment=DEV)

次に、サーブレットリスナー(StartListenerと呼ばれます)で、JSFがこれらのプロパティ、マネージドBean、およびjspビューにアクセスできるようにするために次のことを行います。(具体的には、 cfg.skin.richSelectorというプロパティを試してみます)。

public class StartListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        //Environment properties
        Map<String, String> vblesEntorno = System.getenv();

        //Project properties
        String entorno = vblesEntorno.get("project.environment");
        String ficheroPropiedades = "/WEB-INF/config" + entorno + ".properties";
        try {
            Properties props = new Properties();
            props.load(sc.getResourceAsStream(ficheroPropiedades));

            setSkinRichSelector(sc, props.getProperty("cfg.skin.richSelector"));
        } catch (Exception e) {
            //...
        }
    }

    private void setSkinRichSelector(ServletContext sc, String skinRichSelector) {
        sc.setInitParameter("cfg.skin.richSelector", skinRichSelector); 
    }

    public void contextDestroyed(ServletContextEvent sce) {}

}

JSFマネージドBeanの場合:

public class ThemeSwitcher implements Serializable {

    private boolean richSelector;

    public ThemeSwitcher() {

        richSelector = Boolean.parseBoolean(
            FacesContext.getCurrentInstance().getExternalContext().getInitParameter("cfg.skin.richSelector"));

        if (richSelector) {
            //do A
        } else {
            //do B
        }

    }

    //getters & setters

}

xhtmlページの場合:

<c:choose>
    <c:when test="#{themeSwitcher.richSelector}">
        <ui:include src="/app/comun/includes/themeSwitcherRich.xhtml"/>
    </c:when>
    <c:otherwise>
        <ui:include src="/app/comun/includes/themeSwitcher.xhtml"/>
    </c:otherwise>
</c:choose>

これはすべて問題なく機能しますが、それが最も適切な方法であるかどうか、またはこれを何らかの方法で簡略化できるかどうかを専門家に尋ねたいと思います。

ヒントやアドバイスを事前に感謝します

4

2 に答える 2

1

スプリングBeanで使用している場合は、applicationContext.xmlにproperty-placeholderを残します。

次のように、web.xmlでコンテキスト初期化パラメーターを構成します。

   <context-param>
      <param-name>envProp</param-name>
      <param-value>${project.environment}</param-value>
   </context-param>

また、プロパティファイルをクラスパスのパッケージの下に移動します(注:この変更により、クラスパス*:プレフィックスを使用してアプリケーションコンテキストでリソースを見つける必要があります)

次に、次のようにJSFページにバンドルをロードできます。

<f:loadBundle basename="com.examples.config#{initParam['envProp']}" var="msgs"/>

次のようなものを使用します。

<h:outputText value="#{msgs.cfg.skin.richSelector}" />

ただし、このようにシステムプロパティを設定する代わりに、Ryan Lubkeがブログで言及しているようにJNDIを介してProjectStageを構成し、javax.faces.PROJECT_STAGEコンテキストパラメーターでも同じプロパティを使用できるようにします。

于 2012-06-17T22:49:07.903 に答える
1

使用しているSpringのバージョンによって異なります。それがたまたま最新のSpring3.1である場合は、以下を利用できます@Profile

Springsourceブログ

Springsourceリファレンス

于 2012-06-15T09:21:53.923 に答える