Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative paths in spring classpath resource

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?

like image 376
Mike Q Avatar asked Oct 21 '09 14:10

Mike Q


People also ask

What is classpath resource in spring?

ClassPathResource is a Resource implementation for class path resources. It supports resolution as java. io. File if the class path resource resides in the file system, but not for resources in a JAR.

What is a classpath resource?

A resource is file-like data with a path-like name, which resides in the classpath. The most common use of resources is bundling application images, sounds, and read-only data (such as default configuration). Resources can be accessed with the ClassLoader. getResource and ClassLoader.

What is classpath in spring boot application?

It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.


2 Answers

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

<import resource="classpath*:database/schema.xml"/>
like image 197
Daniel Kleine-Albers Avatar answered Oct 20 '22 20:10

Daniel Kleine-Albers


<import resource="classpath:database/schema.xml"/>
like image 45
Trick Avatar answered Oct 20 '22 20:10

Trick