title: Spring boot - Data Source configuration
body: <p>Am using Spring Boot version 1.2.7 in my project. Planning to use Spring Data JPA and trying to setup Universal connection pool to setup Datasource for mysql JDBC.</p>
  
  <p>Am getting the following error and unable to setup Datasource.</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.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)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:969)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:958)
      at mymantri.MymantriApplication.main(MymantriApplication.java:13)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.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)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      ... 25 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.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)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
      ... 27 more
  </code></pre>
  
  <p>My pom.xml,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.test&lt;/groupId&gt;
      &lt;artifactId&gt;test&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;test&lt;/name&gt;
      &lt;description&gt;Test&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.2.7.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;start-class&gt;test.TestApplication&lt;/start-class&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jersey&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.inject&lt;/groupId&gt;
              &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.oracle&lt;/groupId&gt;
              &lt;artifactId&gt;ucp&lt;/artifactId&gt;
              &lt;version&gt;11.2.0.3&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
              &lt;artifactId&gt;httpcore&lt;/artifactId&gt;
              &lt;version&gt;4.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-lang&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-collections&lt;/groupId&gt;
              &lt;artifactId&gt;commons-collections&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.eaio.uuid&lt;/groupId&gt;
              &lt;artifactId&gt;uuid&lt;/artifactId&gt;
              &lt;version&gt;3.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-core-asl&lt;/artifactId&gt;
              &lt;version&gt;1.9.13&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
              &lt;artifactId&gt;gson&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
              &lt;artifactId&gt;guava&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;net.sf.json-lib&lt;/groupId&gt;
              &lt;artifactId&gt;json-lib&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
              &lt;classifier&gt;jdk15&lt;/classifier&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-cloud-starter-parent&lt;/artifactId&gt;
                  &lt;version&gt;Angel.SR3&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  
  <p>TestApplication.java,</p>
  
  <pre><code>@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, VelocityAutoConfiguration.class })
  public class TestApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(TestApplication.class, args);
      }
  }
  </code></pre>
  
  <p>application-dev.properties,</p>
  
  <pre><code>application.datasource.driverClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
  application.datasource.url=jdbc:mysql://localhost:3306/mymantri
  application.datasource.username=root
  application.datasource.password=root
  application.datasource.initialSize=5
  application.datasource.maxPoolSize=5
  application.datasource.minPoolSize=5
  
  hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
  </code></pre>
  
  <p>DataSource class,</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(entityManagerFactoryRef = "testEntityManagerFactory", transactionManagerRef = "testTransactionManager", basePackages = { "com.mymantri.web.repository"})
  public class ApplicationDataSource {
  
      /** The Constant LOGGER. */
      private static final Logger LOGGER = LoggerFactory
              .getLogger(ApplicationDataSource.class);
  
      /** The Constant TEST_SQL. */
      private static final String TEST_SQL = "select 1 from dual";
  
      /** The pooled data source. */
      private PoolDataSource pooledDataSource;
  
  
      /** The environment. */
      private Environment environment;
  
      /** The Constant CONNECTION_WAIT_TIMEOUT_SECS. */
      private static final int CONNECTION_WAIT_TIMEOUT_SECS = 300;
  
      /**
       * Data source.
       * 
       * @return the pool data source
       */
      @Bean(name = "testDataSource")
      @Primary
      public PoolDataSource testDataSource() {
  
          this.pooledDataSource = PoolDataSourceFactory.getPoolDataSource();
          final String databaseDriver = environment
                  .getRequiredProperty("application.datasource.driverClassName");
          final String databaseUrl = environment
                  .getRequiredProperty("application.datasource.url");
          final String databaseUsername = environment
                  .getRequiredProperty("application.datasource.username");
          final String databasePassword = environment
                  .getRequiredProperty("application.datasource.password");
          final String initialSize = environment
                  .getRequiredProperty("application.datasource.initialSize");
          final String maxPoolSize = environment
                  .getRequiredProperty("application.datasource.maxPoolSize");
          final String minPoolSize = environment
                  .getRequiredProperty("application.datasource.minPoolSize");
  
          try {
              pooledDataSource.setConnectionFactoryClassName(databaseDriver);
              pooledDataSource.setURL(databaseUrl);
              pooledDataSource.setUser(databaseUsername);
              pooledDataSource.setPassword(databasePassword);
              pooledDataSource.setInitialPoolSize(Integer.parseInt(initialSize));
              pooledDataSource.setMaxPoolSize(Integer.parseInt(maxPoolSize));
              pooledDataSource.setMinPoolSize(Integer.parseInt(minPoolSize));
              pooledDataSource.setSQLForValidateConnection(TEST_SQL);
              pooledDataSource.setValidateConnectionOnBorrow(Boolean.TRUE);
              pooledDataSource
                      .setConnectionWaitTimeout(CONNECTION_WAIT_TIMEOUT_SECS);
              // pooledDataSource.setConnectionPoolName(poolName);
          } catch (NumberFormatException e) {
              LOGGER.error("Unable to parse passed numeric value", e);
          } catch (SQLException e) {
              LOGGER.error("exception creating data pool", e);
          }
  
          LOGGER.info("Setting up datasource for user:{} and databaseUrl:{}",
                  databaseUsername, databaseUrl);
          return this.pooledDataSource;
      }
  
      @Bean(name = "testEntityManagerFactory")
      public EntityManagerFactory entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          lef.setDataSource(myMantriDataSource());
          lef.setJpaVendorAdapter(vendorAdapter);
          lef.setPackagesToScan("com.mymantri.web.domain");
          lef.setJpaProperties(additionalProperties());
          lef.setPersistenceUnitName("myMantriPersistenceUnit");
          lef.afterPropertiesSet();
          return lef.getObject();
      }
  
      @Bean(name = "testTransactionManager")
      public PlatformTransactionManager transactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory());
          return transactionManager;
      }
  
      Properties additionalProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.dialect",
                  "org.hibernate.dialect.MySQL5Dialect"); 
          properties.setProperty("hibernate.enable_lazy_load_no_trans",
                  "true");    
          properties.setProperty("hibernate.show_sql","true");
  
          return properties;
      }
  
      /**
       * Sets the environment.
       * 
       * @param environment
       *            the new environment
       */
      @Autowired
      public void setEnvironment(Environment environment) {
  
          this.environment = environment;
      }
  
  }
  </code></pre>
  
  <p>Not sure, what is wrong with this config. Am i missing anything in the SpringBootConfig config.</p>