I have a SpringBoot app. that has to access to different datasources to export data from 1 to another (1 local datasource and another remote datasource)
This is how my persistenceConfig looks like
public class PersistenceConfig { @Bean public JdbcTemplate localJdbcTemplate() { return new JdbcTemplate(localDataSource()); } @Bean public JdbcTemplate remoteJdbcTemplate() { return new JdbcTemplate(remoteDataSource()); } @Bean public DataSource localDataSource(){ HikariConfig config = new HikariConfig(); config.setMaximumPoolSize(getLocalDbPoolSize()); config.setMinimumIdle(5); config.setDriverClassName(getLocalDbDriverClassName()); config.setJdbcUrl(getLocalDbJdbcUrl()); config.addDataSourceProperty("user", getLocalDbUser()); config.addDataSourceProperty("password", getLocalDbPwd()); return new HikariDataSource(config); } @Bean public DataSource remoteDataSource(){ HikariConfig config = new HikariConfig(); config.setMaximumPoolSize(getRemoteDbPoolSize()); config.setMinimumIdle(5); config.setDriverClassName(getRemoteDbDriverClassName()); config.setJdbcUrl(getRemoteDbJdbcUrl()); config.addDataSourceProperty("user", getRemoteDbUser()); config.addDataSourceProperty("password", getRemoteDbPwd()); return new HikariDataSource(config); } } But when I init my app I got this error:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: localDataSource,remoteDataSource I also tried to user qualified beans, as follows:
@Bean(name = "localJdbcTemplate") public JdbcTemplate localJdbcTemplate() { return new JdbcTemplate(localDataSource()); } @Bean(name = "remoteJdbcTemplate") public JdbcTemplate remoteJdbcTemplate() { return new JdbcTemplate(remoteDataSource()); } @Bean(name = "localDataSource") public DataSource localDataSource(){ HikariConfig config = new HikariConfig(); config.setMaximumPoolSize(getLocalDbPoolSize()); config.setMinimumIdle(5); config.setDriverClassName(getLocalDbDriverClassName()); config.setJdbcUrl(getLocalDbJdbcUrl()); config.addDataSourceProperty("user", getLocalDbUser()); config.addDataSourceProperty("password", getLocalDbPwd()); return new HikariDataSource(config); } @Bean(name = "remoteDataSource") public DataSource remoteDataSource(){ HikariConfig config = new HikariConfig(); config.setMaximumPoolSize(getRemoteDbPoolSize()); config.setMinimumIdle(5); config.setDriverClassName(getRemoteDbDriverClassName()); config.setJdbcUrl(getRemoteDbJdbcUrl()); config.addDataSourceProperty("user", getRemoteDbUser()); config.addDataSourceProperty("password", getRemoteDbPwd()); return new HikariDataSource(config); } but then I got this other error:
A component required a bean of type 'org.springframework.transaction.PlatformTransactionManager' that could not be found. - Bean method 'transactionManager' not loaded because @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find a primary bean from beans 'remoteDataSource', 'localDataSource' I also tried
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}) @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}) but then I got
A component required a bean of type 'org.springframework.transaction.PlatformTransactionManager' that could not be found.