0

Tomcat 環境にデプロイされた私の webapp には、${myurl} のような placeHolder を含む Spring 構成ファイルが 1 つあります。プレースホルダーを置き換えるために、 PropertyPlaceholderConfigurater Bean を含む WEB-INF ディレクトリに applicationContext.xml を作成し、その場所を WEB-INF ディレクトリ内のプロパティ ファイルに設定しました。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="placeHolderConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="WEB-INF/my.properties"/>
 </bean>
  <import resource="classpath*:META-INF/springFile1.xml"/>
</beans>

次に、web.xml でコンテキストを指定します。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml
    </param-value>
</context-param>

WEB-INF/my.properties 内 myurl=http://www.google.com

これは springFile1.xml です

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="placeholderPrefix" value="${" />
    <property name="placeholderSuffix" value="}" />
</bean>
<bean id="mycache" class="com.abc.MyURL"
    init-method="start" destroy-method="stop">

    <constructor-arg value="${myurl}" />
</bean>
</beans>

解析されていない値 ${myurl} を取得し続けます

プロパティ ファイルをクラスパス、絶対パス、および WEB-INF に配置しようとしましたが、結果はすべて同じです。

助言がありますか?ありがとう。

4

1 に答える 1

2

xml ファイルを web.xml にリストする代わりに、次のようにインポートしてみてくださいapplicationContext.xml

<import resource="classpath*:springFile1.xml"/>

更新: 子 xml でプレースホルダー コンフィギュアラーを再定義しているようです。したがって、ターゲット プロパティ ファイルを指定する親からのものをオーバーライドします。子 xml から configurer 宣言を削除します。

于 2011-02-16T20:14:08.740 に答える