0

I am deploying Spring 3 app on Tomcat7. I am using OXM unmarshaller so I need to create StreamSource like this:

unmarshaller.unmarshal(new StreamSource(new FileInputStream(dbSetupPath)))

I set value for dbSetupPath like this and it works in unit tests:

@Value("src/main/resources/db-setup.xml")
String dbSetupPath;

When I deploy on Tomcat though, I am getting FileNotFoundException. What is correct path that'll keep my tests passing and Tomcat working? I am deploying exploded:war atm.

Xorty
  • 18,367
  • 27
  • 104
  • 155

4 Answers4

0

When you deploy on tomcat, all your resources folder files goes into .war file in WEB-INF/classes folder.

you need to try

@Value("classpath:/db-setup.xml")

So tomcat will check db-setup.xml in the classpath

For more and appropriate details refer

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html

Section : 2.5.3.1 Java based bean metadata

Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • Unfortunatelly, this didn't help me, I am getting: Caused by: java.io.FileNotFoundException: classpath:\db-setup.xml (The filename, directory name, or volume label syntax is incorrect) – Xorty Jul 18 '12 at 12:29
  • you know I can externalize it properties or wherever, but eventually I have to use String for real path. And that's the problem. – Xorty Jul 18 '12 at 12:44
0

Place your db-setup.xml likes the same as picture

enter image description here

and change this

@Value("db-setup.xml")
String dbSetupPath;
Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • Unfortunatelly, this didn't help me, I am getting: Caused by: java.io.FileNotFoundException: db-setup.xml (The system cannot find the file specified) – Xorty Jul 18 '12 at 12:31
0

I think you can specify path like that one:

${webAppRoot}WEB-INF/db-setup.xml

and put db-setup.xml into WEB-INF folder (or create some subfolder).

Also for correct work of this approach you need to specify in your web.xml such parameter:

<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>webAppRoot</param-value>
</context-param>

Spring will automatically inject to this parameter actual path to your web app root folder.

This is working in my case for logging that's why I think it will work in your case too.

Here is discussion about webAppRootKey

Community
  • 1
  • 1
dimas
  • 6,033
  • 36
  • 29
0

So my solution was to inject the value to the org.springframework.core.io.Resource instead of the plain String like this:

@Value("classpath:/db/db-setup.xml")
Resource dbConfig;

And then I accessed the underlying file and supplied it to the marshaller:

unmarshaller.unmarshal(new StreamSource(dbConfig.getFile());
Xorty
  • 18,367
  • 27
  • 104
  • 155