SpringBoot应用程序。使用JdbcTemplate访问2个数据源
我有一个SpringBoot应用程序。必须访问不同的数据源才能将数据从1导出到另一个(1个本地数据源和另一个远程数据源) 这就是我的persistenceConfig的外观SpringBoot应用程序。使用JdbcTemplate访问2个数据源,spring,spring-boot,spring-data,spring-jdbc,jdbctemplate,Spring,Spring Boot,Spring Data,Spring Jdbc,Jdbctemplate,我有一个SpringBoot应用程序。必须访问不同的数据源才能将数据从1导出到另一个(1个本地数据源和另一个远程数据源) 这就是我的persistenceConfig的外观 public class PersistenceConfig { @Bean public JdbcTemplate localJdbcTemplate() { return new JdbcTemplate(localDataSource()); } @Bean
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);
}
}
但当我初始化我的应用程序时,我遇到了以下错误:
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
我还尝试使用合格的bean,如下所示:
@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);
}
但后来我又犯了另一个错误:
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'
我也试过了
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
但后来我
A component required a bean of type 'org.springframework.transaction.PlatformTransactionManager' that could not be found.
您可以使用bean名称来限定它们:
@Bean(name = "localDataSource")
public DataSource localDataSource() {
...
}
@Bean(name = "remoteDataSource")
public DataSource remoteDataSource() {
...
}
@Autowired
@Qualifier("localJdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("remoteJdbcTemplate")
private JdbcTemplate jdbcTemplate;
请注意:您必须对您的JDBCTemplatebean执行相同的操作-只需给它们一个名称,就可以了。
有关更多信息,请参阅Spring JavaDoc:
当您通过自动连线(@Autowired)在导出服务实现中使用JdbcTemplate bean时,需要使用@Qualifier来限定它们:
@Bean(name = "localDataSource")
public DataSource localDataSource() {
...
}
@Bean(name = "remoteDataSource")
public DataSource remoteDataSource() {
...
}
@Autowired
@Qualifier("localJdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("remoteJdbcTemplate")
private JdbcTemplate jdbcTemplate;
Bean从方法名中获取名称,提供name属性只是使其显式(保持名称与方法名相同)。关于
@Bean(name=“…”)
和@Qualifier
的总体建议没有为我修复错误
我用两个嵌入式数据库建立了示例项目,得到了与aothor相同的错误。Spring建议将其中一个数据源bean注释为@Primary
,事实上,这修复了错误。通常情况下,当其他一些应用程序部件只想查看一个或一个主数据源(如果存在多个数据源)时,就会发生这种情况
似乎更好的解决方案是禁用不需要的自动配置bean,保持其余代码不变:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
或:
取决于正在使用的注释
如果作者不使用任何JPA提供程序并直接使用JdbcTemplate进行操作,那么它可能是一个合适的解决方案。这就是我的解决方案。被接受的答案对我不起作用。