java初始化引导层时出错_java – 使用AbstractRoutingDataSource并尝试设置多个DataSource时出现Spring引导错误...

我是

Spring的新手,我正在开发一个新的应用程序,它需要能够连接到多个可用数据库中的一个.根据用户的凭据,我将确定要连接的数据库,因此我需要能够在运行时动态更改连接.我找到了一个旧的Spring博客,其中概述了这个

here的解决方案,该解决方案主张使用AbstractRoutingDataSource,它基于查找键将getConnection()调用路由到其他DataSource.我试着仔细关注博客,但我一直收到以下错误.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration':

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException:

Error creating bean with name 'dataSource' defined in robb.referencecomponent.Application:

Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException:

Failed to look up JNDI DataSource with name 'dev1DataSource'; nested exception is javax.naming.NoInitialContextException:

Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

这是我的Application.java类的代码:

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@Bean

@ConfigurationProperties(prefix="app.dev1.datasource")

public DataSource dev1DataSource() {

return DataSourceBuilder.create().build();

}

@Bean

@ConfigurationProperties(prefix="app.dev2.datasource")

public DataSource dev2DataSource() {

return DataSourceBuilder.create().build();

}

@Bean

public DataSource dataSource() {

return new RoutingDataSource();

}

public class RoutingDataSource extends AbstractRoutingDataSource {

public RoutingDataSource() {

super();

Map targetDataSources = new HashMap<>();

targetDataSources.put(DbLocation.DEV1, "dev1DataSource");

targetDataSources.put(DbLocation.DEV2, "dev2DataSource");

setTargetDataSources(targetDataSources);

setDefaultTargetDataSource(DbLocation.DEV2);

}

@Override

protected Object determineCurrentLookupKey() {

return ClientContextHolder.getDbLocation();

}

}

}

我在一个属性文件中设置DataSource配置,如下所示:

app.dev1.datasource.url=jdbc:oracle:thin:@dev1_db_url

app.dev1.datasource.username=dev1

app.dev1.datasource.password=XXXXXXX

app.dev1.datasource.driver-class-name=oracle.jdbc.OracleDriver

app.dev2.datasource.url=jdbc:oracle:thin:@dev2_db_url

app.dev2.datasource.username=dev2

app.dev2.datasource.password=XXXXXXX

app.dev2.datasource.driver-class-name=oracle.jdbc.OracleDriver

我命名了我的一个DataSources’dev1DataSource’,它抱怨它无法使用JNDI查找它,但是当我删除RoutingDataSource类和dataSource()bean定义并使dev1DataSource()bean @Primary我能够连接到dev1数据库很好.我不确定在将旧的博客代码移植到我的应用程序中我做错了什么,我知道使用xml在示例中设置bean但是我的设置了Java代码和注释,可能在那里犯了一些错误翻译?

有没有人有在Spring引导中使用AbstractRoutingDataSource的经验并且他们遇到过这种问题?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值