当前位置: 首页 > 知识库问答 >
问题:

没有“repository.PersonRepository”类型的合格bean可用[重复]

桂嘉年
2023-03-14

我试图遵循一个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();
}
package ben.services;

@Service

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

  @Override
  public Iterable<Bde> findAll()
  {

    return personRepository.findAll();
  }

}
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;
  }

}

按建议更新包结构:

共有1个答案

孟选
2023-03-14

您将自己限制在包服务

@ComponentScan("services") 

这等于

@ComponentScan(basePackages = "services")

您需要指定所有包,以便实例化bean

@ComponentScan({"services","repository", "web"})
    null
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);
    }

}
 类似资料: