title: using Spring JdbcTemplate - injecting datasource vs jdbcTemplate
body: <p>As per Spring <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-JdbcTemplate-idioms">documentation</a>, 
  the steps to use Spring JdbcTemplate is as follows:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
          &lt;!-- Scans within the base package of the application for @Components to configure as beans --&gt;
          &lt;context:component-scan base-package="org.springframework.docs.test" /&gt;
  
          &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
              &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;
              &lt;property name="url" value="${jdbc.url}"/&gt;
              &lt;property name="username" value="${jdbc.username}"/&gt;
              &lt;property name="password" value="${jdbc.password}"/&gt;
          &lt;/bean&gt;
  
          &lt;context:property-placeholder location="jdbc.properties"/&gt;
  
      &lt;/beans&gt;
  </code></pre>
  
  <p>And then, </p>
  
  <pre><code>    @Repository
      public class JdbcCorporateEventDao implements CorporateEventDao {
  
          private JdbcTemplate jdbcTemplate;
  
          @Autowired
          public void setDataSource(DataSource dataSource) {
              this.jdbcTemplate = new JdbcTemplate(dataSource);
          }
  
          // JDBC-backed implementations of the methods on the CorporateEventDao follow...
      }
  </code></pre>
  
  <p>Basically, the JdbcTemplate is created inside the Component class using the setter for datasource.</p>
  
  <p>Is there anything wrong with doing it this way instead so that there is exactly ONE instance of jdbcTemplate in the application?</p>
  
  <pre><code>&lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
      p:dataSource-ref="dataSource" 
  /&gt;
  </code></pre>
  
  <p>And then injecting the jdbcTemplate itself directly into the Component</p>
  
  <pre><code>@Repository
  public class JdbcCorporateEventDao implements CorporateEventDao {
      @Resource("jdbcTemplate")
      private JdbcTemplate jdbcTemplate;
  
  
      // JDBC-backed implementations of the methods on the CorporateEventDao follow...
  }
  </code></pre>
  
  <p>Is there a reason why the jdbcTemplate itself must not be injected into the component class directly?</p>
  
  <p>SGB</p>