Spring Boot - 无法配置数据源:未指定“url”属性,无法配置嵌入式数据源

问题描述 投票:0回答:3

我想在我的 SpringBoot(版本 2.4)应用程序中配置两个数据源。

以下是应用程序.属性:

spring.datasource2.url=jdbc:postgresql://localhost:5432/db1
spring.datasource2.username=//username
spring.datasource2.password=//pwd
spring.datasource2.initialization-mode=always
spring.datasource2.driver-class-name=org.postgresql.Driver

spring.datasource1.url=jdbc:postgresql://localhost:5432/db2
spring.datasource1.username=//usrename
spring.datasource1.password=//pwd
spring.datasource1.initialization-mode=always
spring.datasource1.driver-class-name=org.postgresql.Driver

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.id.new_generator_mappings=false
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.h2.console.enabled=true
spring.datasource.continue-on-error=true

spring.servlet.multipart.max-file-size=50MB

spring.servlet.multipart.max-request-size=50MB


project.http.enable=false
project.https.port=9090

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

server.os=LINUX

dl.writeTOFile=false
dl.writeTOConsole=true

下面是我的pom.xml:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- marked the embedded servlet container as provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>2.4.0</version>
        </dependency>

当我运行应用程序时,出现以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

当我将一个数据源添加为 Spring.datasource,将另一个数据源添加为 Spring.datasource1 时,应用程序启动正常,但它仅查看在 Spring.datasource 中配置的数据库。

以下是 DB1 Config 类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( entityManagerFactoryRef = "DB1EntityManagerFactory", transactionManagerRef = "DB1TransactionManager",    basePackages = {
    "com.datasource1.repository" } )
@EntityScan("com.datasource1.entity" )
public class Db1Config {
    
        @Primary
        @Bean
        @ConfigurationProperties("spring.datasource")
        public DataSourceProperties SourceProperties() {
            return new DataSourceProperties();
        }
        
        @Primary
        @Bean( name = "DB1Datasource" )
        @ConfigurationProperties( prefix = "spring.datasource.configuration" )
        public DataSource dataSource()
        {
            return SourceProperties().initializeDataSourceBuilder()
                    .type(HikariDataSource.class).build();
        }
        
        @Primary
        @Bean( name = "DB1EntityManagerFactory" )
        public LocalContainerEntityManagerFactoryBean barEntityManagerFactory( EntityManagerFactoryBuilder builder,
                @Qualifier( "DB1Datasource" ) DataSource dataSource )
        {
            return builder.dataSource(dataSource).packages("com.datasource1.entity")
                    .persistenceUnit("db1").build();
        }
        
        @Primary
        @Bean( name = "DB1TransactionManager" )
        public PlatformTransactionManager barTransactionManager(
                @Qualifier( "DB1EntityManagerFactory" ) EntityManagerFactory barEntityManagerFactory )
        {
            return new JpaTransactionManager(barEntityManagerFactory);
        }
}

以下是 DB2Config:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( entityManagerFactoryRef = "DB2EntityManagerFactory", transactionManagerRef = "DB2TransactionManager",    basePackages = {
    "com.DB2.repository" } )
@EntityScan( "com.DB2.entity" )
public class Db2Config {
    
        @Bean
        @ConfigurationProperties("spring.datasource2")
        public DataSourceProperties DB2SourceProperties() {
            return new DataSourceProperties();
        }

        
        @Bean( name = "DB2Datasource" )
        @ConfigurationProperties( prefix = "spring.datasource2.configuration" )
        public DataSource dataSource()
        {
            return DB2SourceProperties().initializeDataSourceBuilder()
                    .type(HikariDataSource.class).build();
        }
        
        @Bean( name = "DB2EntityManagerFactory" )
        public LocalContainerEntityManagerFactoryBean barEntityManagerFactory( EntityManagerFactoryBuilder builder,
                @Qualifier( "DB2Datasource" ) DataSource dataSource )
        {
            return builder.dataSource(dataSource).packages("com.DB2.entity")
                    .persistenceUnit("db2").build();
        }
        
        @Bean( name = "DB2TransactionManager" )
        public PlatformTransactionManager barTransactionManager(
                @Qualifier( "DB2EntityManagerFactory" ) EntityManagerFactory barEntityManagerFactory )
        {
            return new JpaTransactionManager(barEntityManagerFactory);
        }
}

如何解决这个问题?

spring-boot spring-data-jpa spring-data
3个回答
3
投票

你应该改变

spring.datasource1.url=jdbc:postgresql://localhost:5432/db2

spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/db2

不知道为什么会出现这种情况,文档也没有这么说,但我遇到了这个问题,这对我有用

编辑: 正如@M。 Deinum 在他的评论中提到: 这些

        @ConfigurationProperties("spring.datasource")
        (...)
        @ConfigurationProperties( prefix = "spring.datasource.configuration" )

应改为

        @ConfigurationProperties("spring.datasource1")
        (...)
        @ConfigurationProperties( prefix = "spring.datasource1.configuration" )

2
投票

我在创建“project.jar”后遇到了这个问题 尽管 在 IDE / STS(弹簧工具套件)中它工作正常。

“application.yml”文件中不必要的空格“”可能会导致此问题。

server:
  port: 8085


spring:
 datasource:
  url: jdbc:mysql://localhost:3306/studentdb
  username: root
  password: root
  driver-class-name: com.mysql.cj.jdbc.Driver
 jpa:
   hibernate:
    ddl-auto: update
   show-sql: true
   database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
 application:
  name: STUDENT-SERVICE

而不是调整我的“application.yml”文件
我只是将“application.yml”文件中的所有语句移至
“application.properties”文件并按照“.properties”中的要求格式化语句。

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true

spring.application.name=student-service

server.port=8085

(可以在url末尾添加参数)
(spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false)


0
投票

resources
文件夹中有多个配置/属性文件时,您会收到此错误。例如,如果您有
application.properties
configuration.yml
文件

然后你必须告诉 Spring 使用这两个文件作为属性或配置。默认情况下,spring 仅检测或使用

application.properties
文件。

在主类中,可以这样定义:

System.setProperty("spring.config.additional-location", "classpath:configuration.yml");

类似地将所有配置/属性文件添加到 System.有效!

© www.soinside.com 2019 - 2024. All rights reserved.