Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring中使用驱动程序类配置多个数据源?_Java_Spring_Spring Boot_Mariadb - Fatal编程技术网

Java 如何在Spring中使用驱动程序类配置多个数据源?

Java 如何在Spring中使用驱动程序类配置多个数据源?,java,spring,spring-boot,mariadb,Java,Spring,Spring Boot,Mariadb,为什么以下配置中缺少driverclass spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable spring.datasource.testdb.driver-class-name=org.mariadb.jdbc.Driver spring.datasource.testdb.username=test spring.datasource.testdb.password=test @Configuration publ

为什么以下配置中缺少driverclass

spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable
spring.datasource.testdb.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.testdb.username=test
spring.datasource.testdb.password=test


@Configuration
public class DataSourceConfig {
    @ConfigurationProperties(prefix = "spring.datasource.testdb")
    @Primary
    public DataSource dataSourceTest() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public JdbcTemplate jdbcTemplateTest() {
        return new JdbcTemplate(dataSourceTest());
    }

    //secondary db config to follow
}

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
    </dependency>
</dependencies>
spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable
spring.datasource.testdb.driver类名=org.mariadb.jdbc.driver
spring.datasource.testdb.username=test
spring.datasource.testdb.password=test
@配置
公共类数据源配置{
@ConfigurationProperties(前缀=“spring.datasource.testdb”)
@初级的
公共数据源数据源测试(){
返回DataSourceBuilder.create().build();
}
@豆子
@初级的
公共JdbcTemplate jdbcTemplateTest(){
返回新的JdbcTemplate(dataSourceTest());
}
//要遵循的辅助数据库配置
}
org.springframework.boot
弹簧靴启动器jdbc
org.mariadb.jdbc
mariadb java客户端
结果:

未能配置数据源:“url”属性未指定且 无法配置嵌入式数据源。原因:未能 确定合适的驾驶员等级


这很奇怪,因为我甚至可以进入
org.mariadb.jdbc.Driver
类,因此它显然位于类路径上。

您确定正确加载了属性文件吗?消息明确指定没有指定
url
属性,因此显然
spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable
未读取行-驱动程序类路径与此无关


默认情况下,这些行应该位于
应用程序.properties文件中,您是否在该文件中拥有它们?

看起来您只需要将@Bean注释添加到dataSourceTest()

另外,对于Hikari连接池(默认连接池),url属性是JDBCURL,而不是url。所以改变

spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable

有关更多信息和其他可能的解决方案,请参阅:


希望这有帮助。

是的,它位于
应用程序.properties
中。如果删除
DataSourceConfig
并将配置更改为仅一个主ds,例如
spring.datasource.url=jdbc:mariadb://localhost/mytable
,一切正常。因此,一般来说,设置应该是好的。只是多个ds配置没有…我尝试了,然后应用程序成功启动。但是在访问
JdbcTemplate
时,我在线程“main”java.lang.IllegalArgumentException中得到:jdbcUrl是driverClassName所必需的。我修改了答案,也将url属性更改为jdbc-url。事实上,特别是以下答案似乎提供了解决方案:
spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable
spring.datasource.testdb.jdbc-url=jdbc:mariadb://localhost/mytable