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"/> <import resource="spring-hibernate-properties.xml"/> <import resource="spring-persistence.xml"/>
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"/> <import resource="../WebContent/WEB-INF/spring-persistence.xml"/> <import resource="spring-test-hibernate-properties.xml"/>
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!