【问题标题】:No qualifying bean of type 'repository.PersonRepository' available [duplicate]没有可用的“repository.PersonRepository”类型的合格bean [重复]
【发布时间】:2019-07-08 14:40:16
【问题描述】:

我正在尝试遵循 Spring Boot 示例,我在互联网上搜索了几个小时,但没有找到解决方案。大多数解决方案我发现他们说要使用 @ComponentScan 来扫描包,我是否遗漏了什么,任何 hep 都值得赞赏。

SpringBootApplication 类:

package ben;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"services","repository", "web"}) 
public class SpringBootWebApplication
{
public static void main (String  [] args) {
    SpringApplication.run(SpringBootWebApplication.class, args);
  }

}

PersonRepository 类:

package ben.repository;

@Repository
public interface PersonRepository extends CrudRepository<Bde, Integer> {

}

人员服务:

package ben.services;

import models.Bde;

public interface PersonService
{
  public Iterable <Bde> findAll();
}

PersonServiceImpl:

package ben.services;

@Service

public class PersonServiceImpl implements PersonService
{
  @Autowired
  private PersonRepository personRepository;

  @Override
  public Iterable<Bde> findAll()
  {

    return personRepository.findAll();
  }

}

PersonRest 类:

package ben.web;    
@RestController
public class PersonRest
{
  @Autowired
  //@Qualifier("PersonServiceImpl")
  private PersonService personService;

  @RequestMapping("/person")
  @ResponseBody
  public  Iterable <Bde> findAll() {

    Iterable <Bde> persons=personService.findAll();
    return persons;
  }

}

按照建议更新包结构:

【问题讨论】:

  • 已按照@sfat 和 Wim Deblauwe 的建议进行了更新。仍然有同样的错误

标签: java spring spring-boot


【解决方案1】:

您只是在扫描您的服务包。

试试这个...

 @ComponentScan(basePackages = { "services", "repository" })

【讨论】:

  • 或者只是将所有内容放在您的主应用程序类所在的包的子包中。
【解决方案2】:

您将自己限制在包service

@ComponentScan("services") 

这等于

@ComponentScan(basePackages = "services")

您需要指定所有包才能实例化您的 bean

关于如何扫描所有 bean(服务、存储库和 Web)的示例

@ComponentScan({"services","repository", "web"})

作为替代方案,您可以执行以下操作:

  1. 移动包中的所有内容,SpringBootWebApplication 类将位于该包的根目录。

一个例子:

您的所有应用程序都位于com.yourapp。 如果您将SpringBootWebApplication 放在com.yourapp 中,则不再需要@ComponentScan 注释,并且您的类只需使用@SpringBootApplication 就可以简化:

package com.yourapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}

【讨论】:

    【解决方案3】:

    将您的目录结构更改为以下内容:

    ben---- SpringBootWebApplication.java (pkg: ben)
    |
    ----repository (pkg: ben.repository)
    |      |
    |       ------ PersonRepository
    |
    ----services (pkg: ben.services)
    |
    ----web (pkg: ben.web)
    

    然后更新你的 SpringBootWebApplication 类

    package ben
    
    @SpringBootApplication
    public class SpringBootWebApplication
    {
        public static void main (String  [] args) {
            SpringApplication.run(SpringBootWebApplication.class, args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 2020-02-05
      • 2020-06-10
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多