0

I don't understand why classpath is not working in my code. I'm trying to initialise MatFileReader with the external file resource.

<bean id="contents" class="org.apache.commons.io.IOUtils" factory-method="toString">
  <constructor-arg 
value="classpath:data/nps/power_spectrum/input.mat" 
type="java.io.InputStream" />
</bean>
<bean id="matInput" class="com.jmatio.io.MatFileReader">
  <constructor-arg 
name="fileName" 
value="classpath:data/nps/power_spectrum/input.mat" />
</bean>

classpath is working with the first bean (id=contents). I can read the contents of input.mat file. But, in the second beans (id=matInput). I got FileNotFoundException. It looks like compiler failed to replace the keyword classpath. The constructor of MatFileReader takes String as a parameter. If I use the absolute path instead of classpath, it works. But, I want to use the classpath, how can I fix it?

agthumoe
  • 119
  • 1
  • 11
  • Is ur bean id is `id="matInput"` or `id=matinput` ? – OO7 Mar 20 '15 at 09:32
  • sorry, It is "matInput". But, still having FileNotFoundException – agthumoe Mar 20 '15 at 09:41
  • Have you tried to replace `value="classpath:data/nps/power_spectrum/input.mat"` with `value="classpath:/data/nps/power_spectrum/input.mat"` ? or use `index=0` insetead of `name='fileName'` ? – OO7 Mar 20 '15 at 09:43
  • I've tried both ways, still doesn't work. The thing that makes me confuse is, the first bean with id="contents" is working with the same URI. I can read the contents of that .mat file. But, that MatFileReader take fileName as String. – agthumoe Mar 20 '15 at 09:51
  • Have a look at my answer & give it a try. – OO7 Mar 20 '15 at 09:52

2 Answers2

0

If the constructor of MatFileReader takes a String as parameter, i'm not sure that "classpath:" will be kept. You should only put :

<constructor-arg name="fileName" value="data/nps/power_spectrum/input.mat" />

And then, when you try to initialize your File, you have to make something like that :

URL url = this.getClass().getResource(filename)
File file = new File(url.toURI());

Or

InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream(filename);

The last solution will be to declare filename not as a String but as a Resource. depends on what you need

what you do is working, and the injection is good. If i understand correctly it's more a Java problem. Cause you can't initialize a file with

File f = new File("classpath:input.mat");

You have to choose one of the preceding solution i give.

Hope this help

vincent
  • 1,214
  • 12
  • 22
  • Previously I initialise MatFileReader just like you mentioned. It works. But, I'm trying to understand Spring dependency injection and I want to inject dependency automatically when I call the bean (id="matInput"). – agthumoe Mar 20 '15 at 09:57
  • i edit my solution, i think it's more a Java problem than a Spring problem – vincent Mar 20 '15 at 10:06
0

You can use ClassPathResource to read resources on classpath.

<bean id="tagProviderResource" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="/htmlcleaner.xml" />
</bean>

<util:property-path id="tagProviderFile" path="tagProviderResource.file" />

<bean id="tagProvider" class="org.htmlcleaner.ConfigFileTagProvider">
    <constructor-arg ref="tagProviderFile" />
</bean>

More details on above code is obtained Injecting a File From the Classpath Into a Bean.

Have a look at Inject URL for classpath resource

Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33