Splitting Spring configuration in Grails applications

My colleague Andre wrote an interesting post about this topic already and I have to do a follow up. In one of our projects we followed the assumptions Andre made and we found out that there are some things you might be careful not to stumble over them.

First when experimenting with Spring bean configurations in your Grails application it is useful to change the log level for org.codehaus.groovy.grails.commons to at least warning level. It is configured for error per default which does not show if something is wrong in your XML configuration files.

No on to the problem and my findings. If you try to import Spring XML bean configuration files via your resources.groovy file it would like the following:

beans = { 
  importBeans('file:grails-app/conf/spring/context.xml') 
}

The problem is that this seems to work, but only if you run the application via grails run-app with the embedded servlet container.

As soon as you create a WAR file and deploy it into a tomcat you are getting into trouble. The Spring XML bean configuration files are moved into the folder WEB-INF/spring inside the WAR file. This means the XML files are not reachable via the configured path anymore. The spring folder is not in the classpath so using the classpath: prefix will not work either.

Of course you could make some weird configuration hacks that use a different path in the production environment but the best way in my opinion is to not import Spring XML configurations from the groovy DSL files and vice versa. Just add an additional resources.xml for Spring XML configuration and import other XML files from there with a relative path. You can have both a resources.groovy and a resources.xml side by side.

Leave a Reply