【问题标题】:expected at least 1 bean which qualifies as autowire candidate预计至少有 1 个 bean 有资格作为 autowire 候选者
【发布时间】:2021-01-30 06:04:34
【问题描述】:

由于以下错误而无法运行 springboot 应用程序(它说,“考虑在您的配置中定义一个类型为 'com.example.demo.DAOinter' 的 bean”。这里缺少什么) :-

请查看用于 Repository 注释的 Service 类和 DAOInter 接口的详细信息:-

  1. ServiceBusinessLayer 类

  1. DAOinter 类

当 DAOinter 接口中包含存储库注释时,为什么 autowired 在 ServiceBusinessLayer 类中不起作用?

分享包详情和SpringBootApplication类

【问题讨论】:

  • 请分享SpringBootApplication类的代码
  • 我不记得确切的错误,但可能是因为文件夹结构。如果您将具有 main 方法的类放在另一个文件夹/包中,而不是应该用作 bean 的类的父文件夹/包,应用程序将不会扫描所有类。然后应用程序不知道所有的 bean。考虑一下,还提供您的文件夹结构,我们可以看看这个。
  • 确保DAOiniter在同一个包或者@SpringBootApplication注解的类的子包中。这是必要的,因为组件扫描只发生在同一个包和子包中。
  • 也许,this answer 会有所帮助。
  • 请将代码和日志共享为代码格式的文本,而不是图像。

标签: spring-boot spring-security spring-data-jpa spring-data autowired


【解决方案1】:

所以,问题是我的 pom.xml 中缺少 MySQL 连接器依赖项和 MySQL 版本,因此无法连接到 db,从而导致上述错误。

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>

只要我包含了这个依赖,它就不再抛出上述错误了。

另外,我用下面提到的细节修改了 application.properties:-

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2021-10-21
    • 2019-09-25
    相关资源
    最近更新 更多