title: Spring Java Config - @DependsOn chaining
body: <p>I have a 3.2.14.RELEASE spring app and am using java config to wire and inject the beans. </p>
  
  <p>In my case i need to </p>
  
  <ol>
  <li>set up a SSH Tunnel</li>
  <li>create a DataSource so that it uses the same ssh
  tunnel session </li>
  <li>create a Query class which executes a SQL query using
  a JDBCTemplate</li>
  </ol>
  
  <p>I've setup my ApplicationContext link this with @DependsOn annotations to link the three beans</p>
  
  <pre><code>package com.b.e.kpireport;
  
  @Configuration
  @ComponentScan(basePackages = {"com.b.e.kpireport" })
  public class ApplicationContext {
  
      @Bean(name = "sshTunnel")
      public SSHTunnel getSSHTunnel() {
          return new SSHTunnel();
      }
  
      @Bean(name = "dataSource" )
      @DependsOn("sshTunnel")
      public DataSource getDataSource() {
  
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(driver);
          dataSource.setUrl(jdbcUrl);
          dataSource.setUsername(username);
          dataSource.setPassword(password);
          logger.info("getDataSource():"+jdbcUrl+":"+username+"/"+password+":"+driver);
          return dataSource;
      }
  
      @Bean
      @DependsOn("dataSource")
      public Query getQuery() {
          return new Query();
      }  
  }
  </code></pre>
  
  <p>The Query class looks like</p>
  
  <pre><code>class Query {
  
      private JdbcTemplate jdbcTemplate;
  
      @Autowired
      private DataSource dataSource;
  
      @Autowired
      private SSHTunnel sshTunnel;
  
      public void runQuery() {
          sshTunnel.openSession();
          jdbcTemplate = new JdbcTemplate(dataSource);
          jdbcTemplate.execute(sql);
      }
  </code></pre>
  
  <p>Regardless what i've tried, i see that the dataSource is initialised before the SSHTunnel</p>
  
  <pre><code>Aug 26, 2015 5:20:44 PM com.b.e.kpireport.ApplicationContext getDataSource
  INFO: getDataSource():jdbc:mysql://localhost:3006/centstorage:davidobrien/MnBufeuwncv3eR:com.mysql.jdbc.Driver
  Aug 26, 2015 5:20:44 PM com.b.e.kpireport.SSHTunnel openSession
  INFO: openSession
  </code></pre>
  
  <p>Any suggestions on how I can ensure the correct bean initialisation order?</p>