title: load dataSource from application.properties
body: <p>I am using Spring + Hibernate for a school project.</p>
  
  <p>Everything is working, i am able to get data from the database but i have a question about the database settings.</p>
  
  <p>Is it possible to replace the <code>getDataSource</code> method for a config file? Example application.properties.</p>
  
  <p>How can I implement it in my config file? And is there a specified location for config files?</p>
  
  <p>This is my AppConfig.java</p>
  
  <pre><code>package com.exstodigital.photofactory;
  
  import com.exstodigital.photofactory.dao.impl.UserDAOImpl;
  import org.apache.commons.dbcp2.BasicDataSource;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.orm.hibernate5.HibernateTransactionManager;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.sql.DataSource;
  
  @Configuration
  @EnableTransactionManagement
  public class AppConfig {
      @Autowired
      @Bean(name = "sessionFactory")
      public SessionFactory getSessionFactory(DataSource dataSource) {
          LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
  
          // Models
          sessionBuilder.scanPackages("com.exstodigital.photofactory.model");
  
          return sessionBuilder.buildSessionFactory();
      }
  
      @Bean(name = "dataSource")
      public DataSource getDataSource() {
          BasicDataSource dataSource = new BasicDataSource();
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          dataSource.setUrl("********");
          dataSource.setUsername("********");
          dataSource.setPassword("********");
  
          return dataSource;
      }
  
      @Autowired
      @Bean(name = "transactionManager")
      public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
          return new HibernateTransactionManager(sessionFactory);
      }
  }
  </code></pre>