Failed to import Spring bean definition file using relative path

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 down into different components, so it’s easier to test things like DataSources and Hibernate.

Here is my file structure:

ProjectName ->WebContent ->WEB-INF ->applicationContext.xml ->spring-datasource.xml ->spring-hibernate-properties.xml ->spring-persistence.xml ->test ->us.mn.k12... (Java pkgs with JUnit tests) ->spring-hsqldb-datasource.xml ->spring-test-bean-locations.xml ->spring-test-hibernate-properties.xml ->src ->us.mn.k12... (Java pkgs with production code) 

In WEB-INF / applicationContext.xml, I import the following:

 <import resource="spring-datasource.xml"/> <!-- Production datasource --> <import resource="spring-hibernate-properties.xml"/> <!-- Production hibernate properties --> <import resource="spring-persistence.xml"/> <!-- DAO's, hibernate .hbm.xml mapping files --> 

The application works with the above configuration.

My JUnit tests run using DbUnit and the database in the HSQLDB database. Therefore, my JUnit test refers to spring -test-bean -locations.xml, which has the following:

 <import resource="spring-hsqldb-datasource.xml"/> <!-- HSQLDB datasource for test --> <import resource="../WebContent/WEB-INF/spring-persistence.xml"/> <!-- Production DAO's, hibernate .hbm.xml mapping files --> <import resource="spring-test-hibernate-properties.xml"/> <!-- Hibernate properties for test --> 

That way I can specify the properties of the test data source and hibernate, but reuse the production mapping file for DAO, etc. However, I get an error when running the JUnit test. Here is the relevant part of the exception:

 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 

Now, if I move spring -persistence.xml to / test, so I don’t need to use the relative path and refer to it using <import resource="spring-persistence.xml"/> , then the tests run fine. Therefore, I think the contents of my XML files are fine, but I do not import them with a relative path.

Is there something obvious that I'm wrong about importing a relative path? And maybe the main question is what seems like a reasonable strategy for hacking applicationContext.xml into components to make testing easier?

Thanks!

+8
java spring unit-testing
source share
2 answers

The problem is that everything inside WEB-INF is not accessible to ClassLoader in the usual project settings (and spring uses ClassLoader by default to access resources). There are some hacks for this (for example, context links using the file :) prefix, but they are mostly ugly.

The best practice that I propose is to move context files from WEB-INF and to a dedicated resource directory (src / main / resources, if you have a maven setting). Thus, they will be available for both Webapp ClassLoader and local unit test ClassLoaders.

Read the resources section for a further understanding of the mechanisms involved.

+18
source share

Using

  <import resource="file:**/WebContent/WEB-INF/spring-persistence.xml" /> 

It works in spring 3.2.1.RELEASE. Old versions I'm not sure.

+11
source share

All Articles