[英]How to load file from classpath with Spring 3?
我已經定義了一個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(具有調整的路徑)時,我的應用程序可以正常工作。
如何從類路徑正確加載文件?
我的項目的布局如下所示:
使用ClassPathResource時,無需在文件路徑中指定classpath:
參數 :
path-類路徑中的絕對路徑
在這里, 絕對路徑表示從resources文件夾的根目錄開始,因此只需將其更改為
<bean id="ivsInputResource" class="org.springframework.core.io.ClassPathResource">
<qualifier value="ivs" />
<constructor-arg index="0"
value="IVS90test.csv"/>
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.