February 11, 2004

Spring: Referencing multiple bean config files

For our new project we’re using Spring inside a webapp instead of deploying an EJB container. For now Spring is doing duty as a IoC container, Hibernate wrapper and minimal webapp framework. We’ll probably add some AOP stuff a little later. I’m really happy with it so far and hope they’re able to keep a focus on simplicity as the project matures.

So after a little initial work I wrote a new code generation system. It's much smaller than our existing one -- I'm probably not going to automate the relationship and finder methods -- and generates the domain objects, DAO interfaces and Hibernate DAO implementations from the Hibernate mapping files. It also generates the Spring bean declarations in a separate XML file.

However, I already had a bean configuration file: when deployed using the DispatcherServlet Spring looks for a /WEB-INF/${servlet_name}-servlet.xml file. This contains the Hibernate session factory declaration, datasource connection information, web application mappings, etc. So how to reference the separate XML file create during code generation? (This is probably in the docs somewhere, but I didn't see it at first glance....)

One of the nice things about Spring is that it operates like you think it should. You think: "Well, I just tell Spring to load multiple configurations." Ok, do a little research and see that it's no problem. Cool. Next: "How do I tell Spring about my configuration files?" Answer: where you initialize Spring, from the servlet configuration in web.xml:

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-dao-objects.xml,/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
</servlet>

Works like a charm!

Next: Buying a new server for OpenInteract
Previous: Funny column on bowling