March 01, 2004

Spring: Telling the framework where to load beans

Earlier I discussed how to refer to multiple bean definition files in the DispatcherServlet declaration in your web.xml. But you can also refer to beans not in your WAR file using the classpath: prefix:

<servlet>
    ...
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:com/optiron/cd/dao/spring-dao-objects.xml,...
        </param-value>
    </init-param>
</servlet>

I don't think this is in the docs, or if it is I missed it. (RC2 is to be released today according to Juergen.) I found it out by opening up the org.springframework.web.context.support.XmlWebApplicationContext class and browsing to the loadBeanDefinitions() method. There you see a call:

    reader.loadBeanDefinitions(getResource(this.configLocations[i]))

Ctrl-B that 'getResource()' call and you'll jump to org.springframework.context.support.AbstractApplicationContext where you can easily (it's eight lines of code) dope out how they identify items on the classpath. This process took far longer to type out here than to actually do thanks to IDEA.

What prompted this? I split out the DAO/domain objects into their own entity -- IDEA: "module"; Maven: "project" -- and I didn't want to have to copy the generated spring-dao-objects.xml file over to the webapp. Once again, Spring lets you do the right thing...

Next: Changing a little at a time...
Previous: Remembering why I don't like hardware...