2

I am looking to create a hierarchy based on the Spring configurations. The simplest form is several "core" libraries and several "custom" projects that are able to override the beans in the "core" libraries.

When running very simply unit tests via Maven the "core" configuration isn't able to found causing the test to fail.

final Resource[] resources = applicatonContext.getResources("classpath*:core-*spring.xml");

Returns nothing. It isn't able to find the expected core-one-spring.xml or core-two-spring.xml that are located in my custom projects core dependencies.

Isn't it default behavior of Spring to look into the JARs on the classpath as well? Or is there something special I have to do?

When I run in my IDE (IntelliJ) the tests pass perfectly because the entire project is loaded and they are just files that Spring can find.

UPDATE

Spring is able to find the files if I add them explicitly without wildcards.

@ContextConfiguration({"classpath:core-one-spring.xml", "classpath:core-two-spring.xml", "classpath:custom-spring.xml", "classpath:test-spring.xml"})

or

final Resource[] resources = custom.getResources("classpath:core-one-spring.xml");
Thomas Beauvais
  • 1,546
  • 2
  • 16
  • 30
  • I don't believe it can use wildcards in the middle of the name. I think the `*` can only be used with the `classpath` prefix. – Sotirios Delimanolis Feb 02 '14 at 14:59
  • The prefix has nothing to do with using wildcards in the name. The prefix has to do with how many files are loaded. Prefix `classpath` loads a single (first found) resource and prefix `classpath*` will attempt to load all resources matching the pattern. – Thomas Beauvais Feb 02 '14 at 17:37
  • That's not what I meant, but I was wrong anyway. Are you sure your JUnit test configuration puts all the libraries on the classpath? – Sotirios Delimanolis Feb 02 '14 at 18:01
  • I am pretty sure that is the case but how can I examine the classpath during the JUnit test? – Thomas Beauvais Feb 02 '14 at 19:13
  • Try one of [these](http://stackoverflow.com/questions/17540942/how-to-get-the-class-path-of-running-java-program) in a simple `@Test` method. – Sotirios Delimanolis Feb 02 '14 at 19:15

1 Answers1

3

From the manual

Please note that " classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like " classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK’s ClassLoader.getResources() method which only returns file system locations for a passed-in empty string (indicating potential roots to search).

Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51