Search code examples
springjakarta-eeconfiguration-filesdirectory-structure

spring where to store xml config files


I'm developing a spring project and I've just encountered serious problems with loading XML config files within junit (but they are accessible from the web controllers) - can't load XML files.

I listed my classpath (in junit tests) and found that, among the rest, there are 2 directories included:

/var/www/Java/lyricsBase/build/web/WEB-INF/classes/
/var/www/Java/lyricsBase/build/test/classes/

There is just one file I want to include in my test:

  • /WEB-INF/lyricsBaseApp-servlet.xml

and it imports 3 files below:

  • /WEB-INF/hibernate.xml
  • /WEB-INF/dataSource.xml
  • /WEB-INF/beans.xml

I can clearly see that deploying my project on tomcat copies confg files to /var/www/Java/lyricsBase/build/web/WEB-INF/ABC.xml but this directory is not in the classpath, /var/www/Java/lyricsBase/build/web/WEB-INF/classes is included instead. I read that WEB-INF should not be in the classpath (it's just a Java EE principle). OK - so where should I put my config files to access them easily both from web controllers and junit tests? Any good practices?


Solution

  • Place the config files under WEB-INF/classes on the war package, and they will be loaded into the classpath. Consider deploying your code as an exploded war, so the config files can be changed easily.

    Also consider using maven war project for your packaging. If you do, you can just place your config files under src/main/resources under the source code, and they will be packaged into WEB-INF/classes for you.