title: Spring-boot - How to load resources/datasources?
body: <p>I've been using Spring Boot and deploying the application via a <code>.jar</code> file. I want to load <strong>multiple datasources</strong>. When I was using local tomcat I used to add them to <code>context.xml</code>, but now I'm using the embedded tomcat and was wondering if it's possible to load them via some external file or some way which I would put on the server (UAT/PROD).</p>
  
  <p>I've found a sample of how to do it via Java Class, but I was wondering if it's possible to do it other way as I'd like to leave the user/password out of the <code>.jar</code> and the configurations on a local file on the server. (looks easier/faster for future developments). Maybe using a "<code>datasource.yml</code>" or something similar..</p>
  
  <p>Does anyone know how to achieve that? </p>
  
  <p>Thanks!</p>
  
  <p>My <code>context.xml</code> :</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;Context&gt;
  
      &lt;!-- ABC QA --&gt;
      &lt;Resource
          name="jdbc/abc"
          type="javax.sql.DataSource"
          maxActive="5"
          maxIdle="2"
          username="xxx"
          maxWait="1000"
          validationQuery="select 1"         
          driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
          password="xxx"     
          url="jdbc:sqlserver://server-qa.com:1234;databaseName=myDB"/&gt;
  
      &lt;!-- DEF QA --&gt;
      &lt;Resource
          name="jdbc/def"
          type="javax.sql.DataSource"
          maxActive="5"
          maxIdle="2"
          username="xxx"
          maxWait="1000"
          validationQuery="select 1"         
          driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
          password="xxx"     
          url="jdbc:sqlserver://server2-qa.com:1234;databaseName=myDB"/&gt;
  
  &lt;/Context&gt;
  </code></pre>
  
  <p>Ps.: In case anyone wants to check the java I refer to, the link is: <a href="https://github.com/wilkinsona/spring-boot-sample-tomcat-jndi/blob/master/src/main/java/sample/tomcat/jndi/SampleTomcatJndiApplication.java" rel="nofollow">https://github.com/wilkinsona/spring-boot-sample-tomcat-jndi/blob/master/src/main/java/sample/tomcat/jndi/SampleTomcatJndiApplication.java</a></p>
  
  <h1>Update:</h1>
  
  <p>after a bit of changes this code below now works in case anyone needs it! :)</p>
  
  <p><code>application.yml</code></p>
  
  <pre><code>server:
    port: 9998
  
  logging.level.org.springframework.web: DEBUG
  
  spring.ds_db1:
    url: jdbc:sqlserver://mydb1.com:1433;databaseName=mydb1
    username: xxx
    password: xxx
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
  
  spring.ds_db2:
    url: jdbc:sqlserver://mydb2.com:1433;databaseName=mydb2
    username: xxx
    password: xxx
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
  </code></pre>
  
  <p><code>DatabaseConfig</code></p>
  
  <pre><code>@Configuration
  @ConfigurationProperties(prefix = "spring.ds_db1")
  public class DatabaseDb1Config {
  
      @Bean(name="dsDb1")
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(env.getRequiredProperty("spring.ds_db1.driverClassName"));
          dataSource.setUrl(env.getRequiredProperty("spring.ds_db1.url"));
          dataSource.setUsername(env.getRequiredProperty("spring.ds_db1.username"));
          dataSource.setPassword(env.getRequiredProperty("spring.ds_db1.password"));
  
          return dataSource;
      }
  
      @Bean(name="jdbcDb1")
      public JdbcTemplate jdbcTemplate(DataSource dsChange) {
          return new JdbcTemplate(dsChange);
      }
  
  }
  </code></pre>
  
  <p><code>Repository</code></p>
  
  <pre><code>@Service
  public class ContractRepositoryImpl implements ContractRepository {
  
      @Autowired
      @Qualifier("jdbcDb1")
      protected JdbcTemplate jdbc;
  
      public Contract getContract(long id) {
          return jdbc.queryForObject("SELECT ...", contractMapper, id);
      }
  
      private static final RowMapper&lt;Contract&gt; contractMapper = new RowMapper&lt;Contract&gt;() {
          public Contract mapRow(ResultSet rs, int rowNum) throws SQLException {
              Contract contract = new Contract(rs.getLong("contract"), rs.getString("name"));
              return contract;
          }
      };
  
  }
  </code></pre>
  
  <p>Relevant part from <code>pom.xml</code></p>
  
  <pre><code>...
  &lt;spring.version&gt;4.2.0.RELEASE&lt;/spring.version&gt;
  ...
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- SQL SERVER DRIVER FOR LEGACY CONNECTIONS --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
          &lt;artifactId&gt;sqljdbc4&lt;/artifactId&gt;
          &lt;version&gt;2.0&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>