【问题标题】:Getting an error Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured出现错误无法配置数据源:未指定“url”属性并且无法配置嵌入式数据源
【发布时间】:2022-02-02 12:11:28
【问题描述】:

完整的错误是

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

我在 application.yml 中设置了以下数据源

spring:
  datasource:
    reader:
      username: admin
      password: password
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbcUrl: jdbc:mysql://<aurora-mysql-reader-endpoint_instance>:3306/credentialdb
      pattern: get*,find*
    writer:
      username: admin
      password: password
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbcUrl: jdbc:mysql://<aurora-mysql-reader-endpoint_instance>/credentialdb
      pattern: add*,update*
  jpa:
    hibernate:
      ddl-auto: update

我做了谷歌,排名第一的评论是

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

但是导致下面的代码崩溃

public interface CredentialRepository extends CrudRepository<CredentialEntity, Long> {
}
@Service
public class CredentialServicesImpl implements CredentialServices {
    
    
    @Autowired
    CredentialRepository credentialrepository;

例外

NoSuchBeanDefinitionException: No qualifying bean of type 'com.opsec.shared.harvesting.fetcher.credentialmanger.repository.CredentialRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

那么,我该如何绕过原始异常 配置数据源失败:未指定“url”属性

我认为这可能是一个依赖问题,我正在尝试连接到 AWS RDS aurora mysql,但我的依赖是

 <!-- spring  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- SQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

【问题讨论】:

  • 您是否在 CredentialRepository 中添加了 @Repository 注释
  • 把jdbcUrl改成url
  • 是的,我已经尝试使用@Repository 注释并将 jdbcUrl 更改为 url。我觉得它可能与 pom 相关,缺少依赖

标签: java mysql spring-boot amazon-aurora


【解决方案1】:

好的,数据源为

spring:
  datasource:
    url: jdbc:mysql://<aurora-mysql-reader-endpoint_instance>:3306/credentialdb?sslMode=DISABLED
    username: admin
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect
  hibernate:
    ddl-auto: update

连接极光数据库

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 2019-03-25
    • 2019-08-24
    • 2021-03-18
    • 2019-08-07
    • 2019-10-30
    • 2020-05-31
    • 2023-02-21
    • 2019-09-12
    相关资源
    最近更新 更多