title: spring boot autoconfiguration with jdbc template autowiring dataSource issue
body: <p>I'm new to Spring and to J2EE in general. I'm having trouble using JDBC template with Spring Boot autoconfiguration.</p>
  
  <p>What I did was I took the example of RESTful web service provided <a href="http://spring.io/guides/gs/rest-service/" rel="noreferrer">here</a>
  and decided to extend it to use JDBC template relational database access. Unfortunately another <a href="http://spring.io/guides/gs/relational-data-access/" rel="noreferrer">example</a> provided cannot be useful because the only difficulty which is providing dataSource from xml beans file is not considered.</p>
  
  <h2>What I tried to solve the issue:</h2>
  
  <ol>
  <li>Using DAO Impl class as extend of different implementations from Spring.</li>
  <li>Adding  to beans file.</li>
  <li>Using different DataSource classes (eg DriverManagerDataSource).</li>
  <li>Trying to autowire just a simple attribute in a different class (something less complex then data source).</li>
  <li>On the beggining I just written DAO class, but then I though that maybe it is possible to autowire datasource only if it implements an interface, tryed it, didn't help.</li>
  </ol>
  
  <p>I tryed everything I found on Stack or Google. Most examples are seriously outdated or unanswered or have nothing to do with Spring Boot Autoconfiguration et cetera.</p>
  
  <p>I keep getting <code>Property 'dataSource' is required</code> error, after struggling if finally managed to link the <code>application-config.xml</code> file with beans, but can't manage to autowire the datasource for JDBC.</p>
  
  <p>I'm desperade to finish it and seriously blocked, out of ideas, I would be greatfull if somebody could provide a recent example that works with Spring Boot Autoconfigurations, beans in XML find, autowired datasource for JDBC.</p>
  
  <p>Or at least some ideas, clues, even how to look for it, because I'm even out of keywords for google'ing.</p>
  
  <p>Thanks!</p>
  
  <p><img src="https://i.stack.imgur.com/Qt2WZ.png" alt="enter image description here"></p>
  
  <h2>Spring Application class.</h2>
  
  <pre><code>package ws;
  
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.SpringApplication;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.ImportResource;
  
  @Configuration
  @ComponentScan
  @ImportResource("classpath:spring/application-config.xml")
  @EnableAutoConfiguration
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <h2>Web Service class.</h2>
  
  <pre><code>package ws;
  
  import dao.UserDAOImpl;
  import model.User;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class TestWS {
      @RequestMapping("/greeting")
      public User greeting(@RequestParam(value="name", defaultValue="World") String name) {
      return new User("ubububu", "661331555", 0);
      }
      @RequestMapping("/create")
      public String initialize() {
          UserDAOImpl users = new UserDAOImpl();
          users.init();
          return "seems ok";
      }
  }
  </code></pre>
  
  <h2>DAO interface</h2>
  
  <pre><code>package dao;
  
  import model.User;
  
  public interface UserDAO {
      public void insert(User usr);
      public void init();
      public User select(int id);
  }
  </code></pre>
  
  <h2>DAO implementation</h2>
  
  <pre><code>package dao;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.stereotype.Repository;
  
  import model.User;
  import dao.UserDAO;
  
  @Repository
  public class UserDAOImpl implements UserDAO {
      private JdbcTemplate jdbcTemplate;
      private DriverManagerDataSource dataSource;
      @Autowired
      public void setDataSource(DriverManagerDataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      public void insert(User usr) {
          String sql = "INSERT INTO USER " +
                  "(USR_ID, EMAIL, PHONE) VALUES (?, ?, ?)";
  
          this.jdbcTemplate = new JdbcTemplate(dataSource);
          jdbcTemplate.execute(sql);
      }
  
      public void init() {
          String sql = "CREATE TABLE USER (USR_ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,EMAIL VARCHAR(30) NOT NULL,PHONE VARCHAR(15) NOT NULL)";
          this.jdbcTemplate = new JdbcTemplate(dataSource);
          jdbcTemplate.execute(sql);
      }
  }
  </code></pre>
  
  <h2>Data model</h2>
  
  <pre><code>package model;
  
  public class User {
      private String email;
      private String phone;
      private int id;
      public User(String email, String phone, int id) {
          this.email = email;
          this.phone = phone;
          this.id = id;
      }
      public int getUsrId(){
          return this.id;
      }
      public String getUsrEmail() {
          return this.email;
      }
      public String getUsrPhone() {
          return this.phone;
      }
  }
  </code></pre>
  
  <h2>Configuration bean file</h2>
  
  <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.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
      &lt;import resource="Spring-User.xml" /&gt;--&gt;
      &lt;context:component-scan base-package="ws"/&gt;
      &lt;bean id="ds" 
           class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
          &lt;property name="driverClassName" value="org.springframework.jdbc.core.JdbcTemplate" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/:3306/databasename" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="password" /&gt;
      &lt;/bean&gt;
      &lt;bean id="UserDAOprovider" class="dao.UserDAOImpl"&gt;
          &lt;property name="dataSource" ref="ds" /&gt;
      &lt;/bean&gt; 
  
  &lt;/beans&gt;
  </code></pre>
  
  <h2>Error message:</h2>
  
  <pre><code>ERROR [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required] with root cause
  java.lang.IllegalArgumentException: Property 'dataSource' is required
      at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:135) ~[spring-jdbc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.&lt;init&gt;(JdbcTemplate.java:169) ~[spring-jdbc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at dao.UserDAOImpl.init(UserDAOImpl.java:66) ~[demo3-0.0.1-SNAPSHOT.jar!/:na]
      at ws.TestWS.initialize(TestWS.java:30) ~[demo3-0.0.1-SNAPSHOT.jar!/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_33]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.6.0_33]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.6.0_33]
      at java.lang.reflect.Method.invoke(Method.java:622) ~[na:1.6.0_33]
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) ~[spring-web-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) ~[spring-webmvc-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.0.8.RELEASE.jar!/:4.0.8.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) ~[na:1.6.0_33]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.6.0_33]
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-7.0.57.jar!/:7.0.57]
      at java.lang.Thread.run(Thread.java:701) ~[na:1.6.0_33]
  </code></pre>