gpt4 book ai didi

SpringBoot 应用程序。使用 JdbcTemplate 访问 2 个数据源

转载 作者:行者123 更新时间:2023-12-02 06:57:30 25 4
gpt4 key购买 nike

我有一个 SpringBoot 应用程序。必须访问不同的数据源才能将数据从 1 导出到另一个(1 个本地数据源和另一个远程数据源)

这就是我的 persistenceConfig 的样子

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() {
...
}

请注意:您必须对 JdbcTemplate beans 执行相同的操作 - 只需给它们一个名称,它就会起作用。有关更多信息,请参阅 Spring JavaDoc:Bean

@Bean(name = "localJdbcTemplate")
public JdbcTemplate localJdbcTemplate() {
return new JdbcTemplate(localDataSource());
}

当您通过 Autowiring (@Autowired) 在导出服务实现中使用 JdbcTemplate bean 时,您需要使用 @Qualifier 来限定它们:

@Autowired
@Qualifier("localJdbcTemplate")
private JdbcTemplate jdbcTemplate;

@Autowired
@Qualifier("remoteJdbcTemplate")
private JdbcTemplate jdbcTemplate;

关于SpringBoot 应用程序。使用 JdbcTemplate 访问 2 个数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48803947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com