2011-12-27 19 views
0

私は本当にこの1つに固執しています...ヘルプ! :)<jee:jndi-lookupデフォルト値とクラスパスの使用

私はj2ee:jndiルックアップをプロパティファイルに使用しています。以下では正常に動作します:

<bean class="org.springframework.core.io.FileSystemResource"> 
    <constructor-arg> 
     <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true" /> 
    </constructor-arg> 
</bean> 

はしかし、私は、JNDIルックアップが失敗した場合に対処したいが、WEB-INF/classesフォルダにあるデフォルトのファイルにフォールバックします。しかし

<bean class="org.springframework.core.io.FileSystemResource"> 
    <constructor-arg> 
     <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true" 
      default-value="classpath:myprops.properties" /> 
    </constructor-arg> 
</bean> 

、Iハードコードデフォルト値のための特定のパスの場合:私は以下のようにデフォルト値を使用している場合、Webアプリケーションは、ファイル「myprops.propertiesクラスパス」を見つけることができないことを訴えて例外がスローされます、それは正常に動作しますが、それは最終的な解決策として受け入れられません。

私の問題は「classpath:」を使って正しく解決されるようにすることです。

これは私が使用しています全体的な使い方です:

<bean id="authServerProperties" 
    class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="ignoreResourceNotFound" value="true"/> 
    <property name="location"> 
     <bean class="org.springframework.core.io.FileSystemResource"> 
      <constructor-arg> 
       <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true" 
        default-value="classpath:myprops.properties" /> 
      </constructor-arg> 
     </bean> 
    </property> 
    ..... 
</bean> 

答えて

1

は春ではなく、これはしませんよう、明示的なFileSystemResource豆を供給するよりも、リソースの種類を決定するために、そのビルトインPropertyEditorサポートを使用してみましょうクラスパスリソースを処理します(ファイルシステム上のパスで設定する必要があります)。代わりに、ここでは、文字列値に場所を設定し、春には、適切なリソースタイプにそれを変換することができますので、あなたがあなたのweb.xml

<env-entry> 
    <env-entry-name>myProps</env-entry-name> 
    <env-entry-type>java.lang.String</env-entry-type> 
    <env-entry-value>file:///Users/something/myProps.properties</env-entry-value> 
</env-entry> 

を持っている場合している

<bean id="authServerProperties" 
     class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="ignoreResourceNotFound" value="true"/> 
    <property name="location" ref="myProps" /> 
</bean> 

<jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true" 
      default-value="classpath:myprops.properties"/> 

のようなものを使用する必要があります指定されたファイルURLにUrlResourceを使用します。そうでない場合は、ファイルmyprops.propertiesを検索するためにClasspathResourceが作成されます。

関連する問題