Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put spring configuration files when using maven

I'm trying to solve my configuration-files-problem.

I have Spring MVC application with this structure (my current structure)

src/main/java/ ....................... (java classes)
src/main/resources/ .................. (some .properties files )
src/main/webapp/ ..................... (web content, web.xml )
src/main/webapp/spring/ .............. (other spring config files, app-confix.xml, etc.)
src/main/webapp/myApp-servlet.xml .... (servlet config)


src/test/java ........................ (java test classes)
src/test/resources ................... (some test .properties files) 

(that is pretty standard maven project directory structure)

So, and my question is: Should I put spring configuration files to resources directory or not ? I'm not sure if they can be considered as resource files. Most of the spring projects have configuration in WEB-INF/ directory. But there is one problem with unit testing, because when I need to load servlet configuration then is maven expecting configuration files in /test/resources directory. I know, that I can copy context xml file to the tested class and everything will be ok, but that not look so good for me.

I was reading a lot of articles on the internet, but I'm still not sure what is the right way.

So I have tree options:

1. stay with current structure

2. move all spring configuration to the /main/(java|test)/resources Here I can see one bad thing. All configuration files will be in classes directory and I'm not sure if this is right.

3. move only servlet config to the /main/(java|test)/resources and keep other spring configuration files in /WEB-INF/ directory.

So, what do you recommend ? What is the best practices for configuration files placement when using Spring with Maven and JUnit ?

UPDATE: fixed directory naming in option 2. and 3.

UPDATE: I found very interesting reading [1] about best practices in Spring. It looks very promising and confirming Ralph's solution. I'm still not happy about presence META-INF directory in WEB-INF/classes but, well ... nothing is perfect.

[1] http://gordondickens.com/wordpress/2012/07/03/enterprise-spring-best-practices-part-1-project-config/

like image 932
Peter Avatar asked Dec 21 '22 12:12

Peter


1 Answers

A common pattern (for example used by Spring Roo applications) is to put the configuration files under src\main\resources\META-INF\spring\

Then you can load them via the same expression classpath:META-INF\spring\application-context.xml in tests as well as in your application.

like image 87
Ralph Avatar answered Jan 16 '23 13:01

Ralph