Root contexts prefer not to share

Tonight I learned something funny with Spring: Don't be lazy when defining your root application context. I wanted to play around with Jersey and its integration with Spring, but was kind of lazy:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/SpringMVC-servlet.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet>
        <servlet-name>spring-jersey</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
        </servlet-class>
    </servlet>

This is a really bad idea because you will end up with both the REST Resources as well as everything in Spring being able to access the same beans (those defined in the SpringMVC-servlet.xml) but with actually 2 contexts. If you, for instance, try to access a singleton bean called "dataService" from within a Resource and then also from within a SpringMVC controller, the hashCode of that so-called "singleton" will be different …

So don't be lazy and give your root application context its own bean configuration if you intend to put something like a connection pool or something that really should be singleton in there ;-)