I have a 3.2.14.RELEASE spring app and am using java config to wire and inject the beans.
In my case i need to
- set up a SSH Tunnel
- create a DataSource so that it uses the same ssh tunnel session
- create a Query class which executes a SQL query using a JDBCTemplate
I've setup my ApplicationContext link this with @DependsOn annotations to link the three beans
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();
}
}
The Query class looks like
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);
}
Regardless what i've tried, i see that the dataSource is initialised before the SSHTunnel
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
Any suggestions on how I can ensure the correct bean initialisation order?