title: Spring app doesn't find datasource in config files
body: <p>When I deploy my spring app (Spring 2.5.6) onto a Tomcat (6.0), start up fails with </p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [C.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
  </code></pre>
  
  <p>My web.xml looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="[snip]" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  &lt;display-name&gt;[snip]&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
     [snip]
  &lt;/welcome-file-list&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:All.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;[snip]&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;[snip]&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>All.xml imports a number of configuration files:</p>
  
  <pre><code>&lt;import resource="A.xml"/&gt;
  &lt;import resource="B.xml"/&gt;
  &lt;import resource="C.xml"/&gt;
  ...
  </code></pre>
  
  <p>B.xml defines a data source:</p>
  
  <pre><code>&lt;jee:jndi-lookup id="dataSource" jndi-name="[snip]"/&gt;
  </code></pre>
  
  <p>C.xml creates a hibernate session factory, referencing the data source from B.xml:</p>
  
  <pre><code>&lt;bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="mappingResources"&gt;
          &lt;list&gt;
              &lt;value&gt;[snip]&lt;/value&gt;
              &lt;value&gt;[snip]&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
    &lt;props&gt;
         [snip]
    &lt;/props&gt;
  &lt;/property&gt;
  &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>When I deploy my WAR file, above exception occurs. My questions are: </p>
  
  <ol>
  <li>Why?</li>
  <li><p>Why does the same exception not occur in my JUnit test that I wrote to test the validity of the spring configuration? Here's the test:</p>
  
  <p>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "/All.xml" })
  public class ImfSpringConfigurationTest {
    [snip]]
  }</p></li>
  </ol>
  
  <p>Note: In the JUnit test, I use a different All.xml file to replace B.xml with a file that defines a data source without jndi lookup, like so:</p>
  
  <pre><code>&lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName = "oracle.jdbc.driver.OracleDriver"
      [snip] /&gt;
  </code></pre>
  
  <p>**</p>
  
  <p>Turns out, this configuration is correct. It was the deploy of another module operating on some of the same config files that threw the exception, not this deploy process.</p>