title: Configuring Datasource Properties
body: <p>I am having trouble setting my datasource properties in my java bean configuration class. It appears that the properties aren't being read in properly from the yaml file. I am getting a null pointer exception when the driver class name is attempted to be accessed.</p>
  
  <p>This is my java bean config file:</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Autowired
      private DataSourceProperties properties;
  
      //other beans here
  
      @Bean
      public DataSource dataSource(){
          HikariDataSource dataSource = new HikariDataSource();
          dataSource.setDriverClassName(properties.getDriverClassName());
          dataSource.setJdbcUrl(properties.getUrl());
          dataSource.setUsername(properties.getUsername());
          dataSource.setPassword(properties.getPassword());
  
      @Bean
      public DataSourceProperties properties(){
          return new DataSourceProperties();
      }
  }
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      //other beans here
  
      @Bean
      public DataSource dataSource(DataSourceProperties properties){
          HikariDataSource dataSource = new HikariDataSource();
          dataSource.setDriverClassName(properties.getDriverClassName());
          dataSource.setJdbcUrl(properties.getUrl());
          dataSource.setUsername(properties.getUsername());
          dataSource.setPassword(properties.getPassword());
  
          return dataSource;
      }
  
      @Bean
      public DataSourceProperties properties(){
          return new DataSourceProperties();
      }
  
  }
  </code></pre>
  
  <p>I also tried to use springframeowrk.jdbc's DriverManagerDataSource instead of Hikari, but it still wouldn't work.</p>
  
  <p>This is my application.yml file in src/main/resources:</p>
  
  <pre><code>spring:
      datasource:
          driverClassName: 'com.ibm.db2.jcc.DB2Driver'
          username: 'appUsername'
          password: 'appPw'
          url: 'datasourceURL'      
  </code></pre>
  
  <p>Has anyone had a similar problem to this and would know how to set get the properties set correctly?</p>