Relative paths in spring classpath resource

39,492

Solution 1

A short addition: If you want to access the resources from a jar, it should read:

<import resource="classpath*:database/schema.xml"/>

Solution 2

<import resource="classpath:database/schema.xml"/>

Solution 3

What works fine on my project is the following lines in the app-servlet.xml :

<!-- Database Configuration -->
<import resource="classpath*:/database/DataSource.xml"/>
<import resource="classpath*:/database/Hibernate.xml"/>

Directories

If it can help you ...

Share:
39,492
Mike Q
Author by

Mike Q

Updated on September 09, 2020

Comments

  • Mike Q
    Mike Q over 3 years

    I have a bunch of spring config files, all of which live under the META-INF directory in various subpackages. I have been using import like the following...

      <import resource="../database/schema.xml"/>
    

    So a relative path from the source file. This works fine when I am working with a local build outside of a jar file. But when I package everything up in a jar then I get an error that it cannot resolve the URL resource. If I change the above to an absolute path (with classpath:) then it works fine.

    Is there any way to use relative paths with ".." in when the configs are packaged in a jar or am I restricted to descending relative paths and absolute paths only?

  • Mike Q
    Mike Q over 14 years
    It would need to be <import resource="classpath:../database/schema.xml"/> but this doesn't work.
  • Trick
    Trick over 14 years
    Hm... You have to have it in META-INF?
  • Mike Q
    Mike Q over 14 years
    Not necessarily but I need it packaged in the jar so users of the jar pick up the config with it.
  • Trick
    Trick over 14 years
    My structure for jars looks like so: src/main/java/ - code src/main/resources/ - configurations files (including spring)
  • Gerben Rampaart
    Gerben Rampaart almost 7 years
    The asterix! That's what I was missing.