title: Why DataSource cannot be autowired in spring boot application?
body: <p>I know that spring boot will create a <code>dataSource</code> Bean automatically if related configurations are set in <code>application.properties</code>, like:</p>
  
  <pre><code>spring.datasource.url = jdbc:mysql://192.168.10.103:3306/hms?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull
  spring.datasource.username=root
  spring.datasource.password=test@123
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  </code></pre>
  
  <p>Application code：</p>
  
  <pre><code>package com.synline.mdataserver;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.apache.tomcat.jdbc.pool.DataSource;
  
  @SpringBootApplication
  public class Application implements CommandLineRunner {
  
      @Autowired
      AnnotationConfigApplicationContext context;
  
      /*@Autowired
      DataSource dataSource;*/
  
      public static void main(String[] args) throws InterruptedException {
          SpringApplication.run(Application.class, args);
      }
  
      @Override
      public void run(String... args) throws Exception {
          DataSource dataSource = (DataSource)context.getBean("dataSource");
          System.out.println(dataSource);
  
          while (true) {
             Thread.sleep(5000);
          }
  
      }
  }
  </code></pre>
  
  <p>If the @Autowired DataSource is commented out, the Bean information will be printed:</p>
  
  <pre><code>org.apache.tomcat.jdbc.pool.DataSource@1800a575{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; ....}
  </code></pre>
  
  <p>So I think Spring Boot really created the Bean.</p>
  
  <p>But if @Autowried DataSource is used, exception occurs to complain No Such Bean</p>
  
  <pre><code>Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.apache.tomcat.jdbc.pool.DataSource com.synline.mdataserver.Application.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tomcat.jdbc.pool.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>