title: Will spring Boot datasource properties work if we configure datasource programmatically in Spring Boot
body: <p>Will Spring Boot datasource properties work if we configure datasource programmatically?</p>
  
  <p>The following properties worked only when I fetch DB configuration from application.properties. If I configure datasource programmatically the following properties are not working.</p>
  
  <pre><code>spring.datasource.tomcat.initial-size=10
  spring.datasource.tomcat.max-active=10
  spring.datasource.tomcat.max-idle=5
  spring.datasource.tomcat.min-idle=5
  </code></pre>
  
  <p>I used the following code to configure datasource programmatically</p>
  
  <pre><code>@ConfigurationProperties(prefix = "spring.datasource")
  @Bean
  @Primary
  public DataSource dataSource() {
      return DataSourceBuilder.create().username(userName).password(password).url(url).driverClassName(driverName)
              .build();
  }
  </code></pre>
  
  <p>To make it work programmatically I used the following code snippet.But I'm not convinced. I feel it is not a cleaner solution. I have to read at least 20 properties from application.properties and add it to PoolProperties. </p>
  
  <pre><code>    @ConfigurationProperties(prefix = "spring.datasource")
      @Bean
      @Primary
      public DataSource dataSource() {
          PoolProperties poolProperties = new PoolProperties();
          poolProperties.setUrl(url);
          poolProperties.setDriverClassName(driverName);
          poolProperties.setUsername(userName);
          poolProperties.setPassword(password);
          poolProperties.setTestWhileIdle(false);
          poolProperties.setTestOnBorrow(true);
          poolProperties.setValidationQuery("SELECT 1 FROM DUAL");
          poolProperties.setTestOnReturn(false);
          poolProperties.setValidationInterval(30000);
          poolProperties.setTimeBetweenEvictionRunsMillis(30000);
          poolProperties.setInitialSize(10);
          poolProperties.setMaxActive(10);
          poolProperties.setMaxIdle(5);
          poolProperties.setMinIdle(5);
          poolProperties.setMaxWait(10000);
          poolProperties.setRemoveAbandonedTimeout(60);
          poolProperties.setMinEvictableIdleTimeMillis(30000);
          poolProperties.setLogAbandoned(true);
          poolProperties.setRemoveAbandoned(true);
          DataSource datasource = new DataSource(); // import org.apache.tomcat.jdbc.pool.DataSource;
          datasource.setPoolProperties(poolProperties);
          return datasource;
      }
  </code></pre>
  
  <p>It there way we can make the following default Spring Boot properties work?</p>
  
  <pre><code>spring.datasource.tomcat.initial-size=10
  spring.datasource.tomcat.max-active=10
  spring.datasource.tomcat.max-idle=5
  spring.datasource.tomcat.min-idle=5
  </code></pre>