首页 > 解决方案 > 没有可用的“repository.PersonRepository”类型的合格 bean

问题描述

我正在尝试遵循 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;
  }

}

包结构更新如建议:

在此处输入图像描述

标签: javaspringspring-boot

解决方案


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

尝试这个...

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

推荐阅读