【问题标题】:Bean of Interface type could not be found找不到接口类型的 Bean
【发布时间】:2020-02-13 01:04:16
【问题描述】:

首先我想说我已经阅读了类似的问题并实施了他们的答案,但似乎没有任何效果。 TL; DR 我正在实施“Spring”一书中的一个例子,这是我的问题。 初级班:

package com.example.demo;
@SpringBootApplication(scanBasePackages = {"com.example.demo", "com.example.demo.dao", "com.example.demo.model", "com.example.demo.service"})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

控制器类:

package com.example.demo.api;
@Controller
@RequestMapping("/spittles")
public class SpittleController {
    private SpittleRepository a;
    @Autowired
    public SpittleController(@Qualifier("abc") SpittleRepository a){
        this.a = a;
    }
}

界面:

package com.example.demo.dao;
@Repository("abc")
public interface SpittleRepository {
    List<Spittle> findSpittles(long max, int count);
}

我的输出:

Description:
Parameter 0 of constructor in com.example.demo.api.SpittleController required a bean of type 'com.example.demo.dao.SpittleRepository' that could not be found.
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Qualifier(value="abc")

正如我之前所说,该示例实际上是从书中复制的(出于问题的目的,我对其进行了简化)。该程序不抛出错误的唯一情况是我在 DemoApplication 类中添加注释:@ComponentScan("com.example.demo.dao")(但只有在 dao 中有一个文件时才有效)。或者,当我将 interface 关键字替换为 class 关键字时,它也可以工作。

有没有办法让类(DemoApplication)找到这个接口?

【问题讨论】:

  • 1.@SpringBootApplication 将对com.example.demo 下的所有包进行组件扫描,因此不需要明确提及基础包。 2.创建一个类,实现接口SpittleRepository,并用@Component注解新类。运行代码,让我们知道结果
  • 2.我同意,我已经尝试过这个解决方案并且它有效,但是不可能只自动连接接口吗? (书中的那个人就是这么做的)
  • 请确认在教程中存储库是否扩展了类似public interface SpittleRepository extends JpaRepository&lt;Spittle, Long&gt; {}
  • 我会引用这本书:" 首先,我们将定义一个数据访问存储库。为了分离数据而不深入研究数据库的细节,我们将定义一个存储库形式为接口,我们稍后会处理实现(在第 10 章。)。此时我们只需要一个接口来下载 spittle 列表“这是书中的代码:public interface SpittleRepository { List&lt;Spittle&gt; findSpittles(long max, int count); }
  • 第 10 章是如何实现的?该接口需要一些实现才能使其工作,不是吗?

标签: java spring spring-boot model-view-controller


【解决方案1】:
  1. @SpringBootApplication 将对 com.example.demo 下的所有包进行组件扫描,因此不需要明确提及基础包。
  2. 创建一个存储库类并实现接口SpittleRepository 并用@Component 注释新类。此处需要接口的实现。

【讨论】:

    【解决方案2】:
    package com.example.demo.dao;
    @Repository
    public interface SpittleRepository {
        List<Spittle> findSpittles(long max, int count);
    }
    

    package com.example.demo.api;
    @Controller
    @RequestMapping("/spittles")
    public class SpittleController {
        private SpittleRepository a;
        @Autowired
        public SpittleController(@Qualifier("spittleRepository ") SpittleRepository a){
            this.a = a;
        }
    }
    

    【讨论】:

    • 遗憾的是,它也抛出了同样的错误:“需要一个无法找到的 'com.example.demo.dao.SpittleRepository' 类型的 bean。”
    【解决方案3】:

    注解@Repository 应该用在类上,而不是接口上。看看这个@Repository

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-16
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 2019-04-02
      相关资源
      最近更新 更多