2012-03-30 4 views
0

私はこのようなPropertyPlaceholderConfigurerを持っている:だから、私は「メインABOVEこのBeanを置く1つのSpring PropertyPlaceholderConfigurerがもう1つを構成できますか?

<context-param> 
    <param-name>runningMode</param-name> 
    <param-value>production</param-value> 
</context-param> 

:私はこのようなweb.xmlに私の走行モードを指定できるようにしたいと思い

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="locations"> 
     <list> 
      <value>classpath:assuredlabor/margarita-${runningMode}.properties</value> 
     </list> 
    </property> 
</bean> 

'プロパティビーン私は上記で説明しました:

<bean id="servletPropertyPlaceholderConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> 
</bean> 

しかし、それは動作していないようです。

は春に、このことは可能ですか?私は今バージョン2.5を使用しています。

私はこれと同様の問題が見つかりました:

PropertyPlaceholderConfigurer with Tomcat & ContextLoaderListener

をしかしそこServletContextPropertyPlaceholderConfigurerのない議論はされていないので、私はそれが正当な質問だと思います。

答えて

1

あなたがいるので、私は考えていないいくつかのカスタムコーディングなしで、春2でこれを行うことはできません1つのプロパティプレースホルダは別のプロパティプレースホルダを構成できません。

あなたはボックスの外にこれを取得するために、スプリング3を使用する必要があります。これを達成するには、何らかの価値があるBeanを作成し、プロパティープレースホルダを設定するときにspring-elを使用してその春を参照する必要があります。そこ下記のショーとして、個々のサーブレットコンテキストパラメータを取得するための特別な豆です:

<bean id="runningMode" class="org.springframework.web.context.support.ServletContextAttributeFactoryBean"> 
    <property name="attributeName" value="runningMode" /> 
</bean> 

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="locations"> 
     <list> 
      <value>classpath:assuredlabor/margarita-#{runningMode}.properties</value> 
     </list> 
    </property> 
</bean> 

は、そしてあなただけの通常の$ {}構文でプロパティのいずれかを参照することができます

1

source codeより:

のServletContext初期化パラメータ(つまり、web.xmlのコンテキスト-PARAMエントリである)としてプレースホルダを解決PropertyPlaceholderConfigurerのサブクラス。

は、web.xmlコンテキストのparamsに加えて、「位置」および/または「プロパティ」値と組み合わせることができます。あるいは、ローカルプロパティなしで定義して、すべてのプレースホルダをweb.xmlコンテキストパラメータ(またはJVMシステムプロパティ)として解決できます。

プレースホルダは、アプリケーション内で提供されるローカルのプロパティに対して解決できなかった場合は、このコンフィギュラはバックのServletContextパラメータに分類されます。 ServletContextのinitパラメータがローカルプロパティをオーバーライドできるように設定することもできます(contextOverride = true)。

オプションでのServletContextを探しサポート属性:オンになっている場合、それ以外の場合は解決できないプレースホルダが見つかった場合、その文字列化値を使用して、対応するServletContext属性と照合します。これは、動的値をSpringのプレースホルダ解決に送るために使用できます。

WebApplicationContext(またはServletContextAwareコールバックを満たすことができるその他のコンテキスト)内で実行されていない場合、このクラスはデフォルトのPropertyPlaceholderConfigurerのように動作します。これにより、テストスイートにServletContextPropertyPlaceholderConfigurer定義を保持することができます。あなただけの単一構成器を使用することができることを意味し

私の理解では、:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="locations"> 
     <list> 
      <value>classpath:assuredlabor/margarita-${runningMode}.properties</value> 
     </list> 
    </property> 
</bean> 
関連する問題