title: Unable to import Spring bean definition file using relative path
body: <p>I am new to Spring and inherited a Spring project that had all the XML configuration in ProjectName/WebContent/WEB-INF/applicationContext.xml.  I'm trying to break the configuration into different components so it is easier to substitute things like DataSources and Hibernate configuation when testing.  </p>
  
  <p>Here is my file structure:</p>
  
  <pre><code>ProjectName
    -&gt;WebContent
        -&gt;WEB-INF
            -&gt;applicationContext.xml
            -&gt;spring-datasource.xml
            -&gt;spring-hibernate-properties.xml
            -&gt;spring-persistence.xml
    -&gt;test
        -&gt;us.mn.k12... (Java pkgs with JUnit tests)
        -&gt;spring-hsqldb-datasource.xml
        -&gt;spring-test-bean-locations.xml
        -&gt;spring-test-hibernate-properties.xml
    -&gt;src
        -&gt;us.mn.k12... (Java pkgs with production code)
  </code></pre>
  
  <p>In WEB-INF/applicationContext.xml, I import the following:</p>
  
  <pre><code>&lt;import resource="spring-datasource.xml"/&gt; &lt;!-- Production datasource --&gt;
  &lt;import resource="spring-hibernate-properties.xml"/&gt; &lt;!-- Production hibernate properties --&gt;
  &lt;import resource="spring-persistence.xml"/&gt; &lt;!--  DAO's, hibernate .hbm.xml mapping files --&gt;
  </code></pre>
  
  <p>The application works with the above configuration.</p>
  
  <p>My JUnit tests run using DbUnit and an HSQLDB in-memory database.  So my JUnit test references spring-test-bean-locations.xml, which has the following:</p>
  
  <pre><code>&lt;import resource="spring-hsqldb-datasource.xml"/&gt; &lt;!-- HSQLDB datasource for test --&gt;
  &lt;import resource="../WebContent/WEB-INF/spring-persistence.xml"/&gt;  &lt;!--  Production DAO's, hibernate .hbm.xml mapping files --&gt;
  &lt;import resource="spring-test-hibernate-properties.xml"/&gt; &lt;!-- Hibernate properties for test --&gt;
  </code></pre>
  
  <p>In this way, I can specify test datasource and hibernate properties, but reuse the production mapping file for the DAO's, etc. However, I get an error running my JUnit test. Here is the relevant part of the exception:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../WebContent/WEB-INF/spring-persistence.xml]
  Offending resource: class path resource [spring-test-bean-locations.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [../WebContent/WEB-INF/spring-persistence.xml]; nested exception is java.io.FileNotFoundException: class path resource [../WebContent/WEB-INF/spring-persistence.xml] cannot be opened because it does not exist
  </code></pre>
  
  <p>Now if I move spring-persistence.xml into /test so that I don't have to use the relative path, and reference it with <code>&lt;import resource="spring-persistence.xml"/&gt;</code>, then the tests run fine. So I think the contents of my XML files are OK, but I'm not properly importing with a relative path.</p>
  
  <p>Is there anything obvious I'm doing wrong with my import of the relative path? And maybe the bigger question is does this look like a reasonable strategy for breaking applicationContext.xml into components to make it easier for testing?</p>
  
  <p>Thanks!</p>