title: Another question on Spring 3, servlet, @autowired
body: <p>I think I've read every question and answer on Spring and autowiring a servlet, both here and at springsource.org, and I still can't get it working.</p>
  
  <p>All I want to do is have the datasource automatically set in my servlets.  I understand that the container creates the servlet and not Spring.</p>
  
  <p>Here is code from my test servlet:</p>
  
  <pre><code>package mypackage.servlets;
  
  imports go here...
  
  @Service
  public class TestServlet extends HttpServlet
  {
    private JdbcTemplate _jt;
  
    @Autowired
    public void setDataSource(DataSource dataSource)
    {
      _jt = new JdbcTemplate(dataSource);
    }
  
    etc etc
  </code></pre>
  
  <p>In my applicationContext.xml I have:</p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="mypackage.servlets /&gt;
  &lt;import resource="datasource.xml" /&gt;
  </code></pre>
  
  <p>and in my datasource.xml:</p>
  
  <pre><code>&lt;jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/db" /&gt;
  </code></pre>
  
  <p>If I can't get this working I'll just use WebApplicationContextUtils in the servlet's init method but I'd really like to make this work after all the reading I've been doing.</p>
  
  <p>I'm using Spring 3, Java 1.6.</p>
  
  <p>Thanks,</p>
  
  <p>Paul</p>