Grails: Splitting resources.groovy

Grails provides a neat way to specify Spring bean definitions with its custom Beans DSL [0]. The bean definition is found in grails-app/conf/spring/resources.groovy:
 
beans = {
    myBean(SomeBeanClass)  {
        someProperty = ref('someReference')
    }
    // ...

}
 
Another way to specify beans is via XML definitions:
 
<beans:beans
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <beans:bean id="myBean" class="org.ast.beans.SomeBeanClass">
        <beans:property name="someProperty" ref="someReference" />
    </beans:bean>
    <!-- ... -->
</beans:beans>
 
These can be specified in grails-app/conf/spring/resources.xml. Grails watches resources.groovy and resources.xml for changes and reloads them at runtime if necessary.

Splitting up with Bean XML files

As the bean definition count raises, it is preferable to split resources.groovy into separate bean definition files. Using Spring's bean definition XML this can be done via the <beans:import/> XML tag:
 
<beans:beans
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <beans:import resource="common.xml"/>
    <beans:import resource="job-definitions.xml"/>
    <!-- ... -->
</beans:beans>
 
It is considered good practice to split up a large Spring bean definition file into multiple files, whereas each file contains beans of the same thematic context (e.g. all beans related to batch jobs go to 'batch-common.xml', all beans related to data-source config goto to 'data-source.xml', etc.).

Splitting up in Beans DSL

So far for XML bean definition files, but what about splitting resources.groovy into separate groovy definitions? Since Grails 1.2, there is a way to do this (although not explicitly documented in the Grails reference documentation). Grails' Beans DSL is interpreted via a grails.spring.BeanBuilder instance which in turn provides method
 
public void importBeans(String resourcePattern) {
// ...

}
 
This method can be used inside the Beans DSL to load arbitrary Spring definitions either specified in XML or Bean DSL groovy files:
 
beans = {
    importBeans('file:grails-app/conf/spring/common.xml)
    importBeans('file:grails-app/conf/spring/job-definitions.xml)
    importBeans('file:grails-app/conf/spring/integration.groovy)
    // ...

}
 
The String parameter of importBeans might be an arbitrary resource pattern which is resolved at runtime using Spring's org.springframework.core.io.support.ResourcePatternResolver, e.g. it would be possible to import multiple XML and/or groovy files using '*' like:
 
importBeans('file:grails-app/conf/spring/*.xml)
 


[0] Grails Beans DSL
[1] BeanBuilder should support import