title: Spring JNDI datasource - no qualifying bean of type [javax.sql.DataSource] found
body: <p>I've defined a JDNI resource in tomcat 7, but Spring is failing to inject it.</p>
  
  <p>Here's my META-INF/context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context&gt;
      &lt;Resource name="jdbc/TyedArtDB"
                auth="Container"
                driverClassName="org.postgresql.Driver"
                factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
                maxActive="10"
                maxIdle="2"
                type="javax.sql.DataSource"
                url="jdbc:postgresql://localhost:5432/tyedart"
                username="user"
                password="pass"/&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>This is the relevant portion of my web.xml:</p>
  
  <pre><code>&lt;resource-ref&gt;
      &lt;description&gt;DB&lt;/description&gt;
      &lt;res-ref-name&gt;jdbc/TyedArtDB&lt;/res-ref-name&gt;
      &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
      &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>Here's my entire servlet-context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"&gt;
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
      &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/TyedArtDB"/&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;annotation-driven /&gt;
  
      &lt;context:component-scan base-package="com.tyedart.web" /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
      &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>And this is the @Autowired bean:</p>
  
  <pre><code>@Autowired
  private DataSource dataSource;
  </code></pre>
  
  <p>For some reason, I get this in the tomcat log:</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency
  </code></pre>
  
  <p>I know my Tomcat JNDI setup is correct because if I comment out the autowired bean and just do this...</p>
  
  <pre><code>InitialContext ctx = new InitialContext();
  DataSource dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/TyedArtDB");
  </code></pre>
  
  <p>...then the dataSource works as expected.</p>
  
  <p>Any ideas what I'm doing wrong?</p>