0

次のように ClassPathResource Bean を定義しました。

<bean id="ivsInputResource" class="org.springframework.core.io.ClassPathResource">
    <qualifier value="ivs" />
    <constructor-arg index="0"
        value="classpath*:IVS90test.csv"/>
</bean>

しかし、リソース Bean が注入されると、私のアプリは次の例外で壊れます。

Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): class path resource [classpath*:IVS90test.csv]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:256)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:134)

明らかに、リソースが見つかりません。FileSystemResource (調整されたパス) を使用すると、アプリは機能します。

クラスパスからファイルを正しくロードする方法は?

私のプロジェクトは次のようにレイアウトされています。

Eclipse でのプロジェクト レイアウトのスクリーンショット

4

1 に答える 1

1

ClassPathResourceclasspath:を使用する場合、ファイルへのパスで指定する必要はありません

パラメータ:
path - クラスパス内の絶対パス

ここで、絶対パスはリソース フォルダーのルートからのパスを意味するため、次のように変更するだけです。

<bean id="ivsInputResource" class="org.springframework.core.io.ClassPathResource">
    <qualifier value="ivs" />
    <constructor-arg index="0"
        value="IVS90test.csv"/>
</bean>
于 2012-11-27T13:56:35.337 に答える