2012-11-27 48 views
0

我已經定義了一個使用ClassPathResource豆像這樣:如何使用Spring 3從classpath加載文件?

<bean id="ivsInputResource" class="org.springframework.core.io.ClassPathResource"> 
    <qualifier value="ivs" /> 
    <constructor-arg index="0" 
     value="classpath*:IVS90test.csv"/> 
</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(調整路徑)我的應用程序確實工作。

如何正確加載類路徑中的文件?

我的項目是奠定了如圖所示:

screenshot of project layout in eclipse

+1

如果使用使用ClassPathResource類,你需要用「類路徑前綴它*:'? – greyfairer

+0

確實,解決了它,如果你把這個評論作爲答案,我可以接受它。 – jorrebor

回答

1

使用ClassPathResource

參數時,您不必指定路徑到您的文件classpath:
路徑 - 類路徑內的絕對路徑

這裏,絕對路徑從資源文件夾的根手段,因此,所有你需要做的是將其更改爲

<bean id="ivsInputResource" class="org.springframework.core.io.ClassPathResource"> 
    <qualifier value="ivs" /> 
    <constructor-arg index="0" 
     value="IVS90test.csv"/> 
</bean> 
相關問題