jorrebor
jorrebor

Reputation: 2232

How to load file from classpath with Spring 3?

I have defined an ClassPathResource bean like so:

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

But when the resource bean is injected, my app breaks with this exception:

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)

Obviously the resource cannot be found. When using FileSystemResource (with adjusted path) my app does work.

How to properly load files from the classpath?

My project is layed out as shown:

screenshot of project layout in eclipse

Upvotes: 0

Views: 5834

Answers (1)

juan
juan

Reputation: 82014

You don't need to specify classpath: in the path to your file when using ClassPathResource

Parameters:
path - the absolute path within the classpath

Here, absolute path means from the root of the resources folder, so all you need to do is change it to

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

Upvotes: 1

Related Questions