Java 没有类型为'的合格bean;repository.PersonRepository';可获得的
我试着遵循一个Spring Boot的例子,我在互联网上搜索了几个小时,却没有找到我的案例的解决方案。我找到的大多数解决方案都是用@ComponentScan来扫描软件包,如果我遗漏了什么,任何hep都会很感激 SpringBoot应用程序类:Java 没有类型为'的合格bean;repository.PersonRepository';可获得的,java,spring,spring-boot,Java,Spring,Spring Boot,我试着遵循一个Spring Boot的例子,我在互联网上搜索了几个小时,却没有找到我的案例的解决方案。我找到的大多数解决方案都是用@ComponentScan来扫描软件包,如果我遗漏了什么,任何hep都会很感激 SpringBoot应用程序类: package ben; @SpringBootApplication @EnableAutoConfiguration @ComponentScan({"services","repository", "web"}) public class Sp
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.repository;
@存储库
公共接口PersonRepository扩展了Crudepository{
}
人事服务:
package ben.services;
import models.Bde;
public interface PersonService
{
public Iterable <Bde> findAll();
}
package-ben.services;
进口型号:Bde;
公共接口人员服务
{
公共可数findAll();
}
PersonServiceImpl:
package ben.services;
@Service
public class PersonServiceImpl implements PersonService
{
@Autowired
private PersonRepository personRepository;
@Override
public Iterable<Bde> findAll()
{
return personRepository.findAll();
}
}
package-ben.services;
@服务
公共类PersonServiceImpl实现PersonService
{
@自动连线
个人知识库;
@凌驾
公共可引用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;
}
}
package-ben.web;
@RestController
公营人员休息
{
@自动连线
//@限定符(“PersonServiceImpl”)
私人私人服务;
@请求映射(“/人”)
@应答器
公共可引用findAll(){
Iterable persons=personService.findAll();
返回人员;
}
}
包结构更新如下所示:
您只是在扫描您的服务包 试试这个
@ComponentScan(basePackages = { "services", "repository" })
您仅限于套餐
服务
@ComponentScan("services")
这等于
@ComponentScan(basePackages = "services")
您需要指定所有包,以便实例化bean
关于如何扫描所有bean(服务、存储库和web)的示例
作为替代方案,您可以执行以下操作:
ben---- SpringBootWebApplication.java (pkg: ben)
|
----repository (pkg: ben.repository)
| |
| ------ PersonRepository
|
----services (pkg: ben.services)
|
----web (pkg: ben.web)
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);
}
}
将目录结构更改为以下内容:
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);
}
}
或者将所有内容放在主应用程序类所在包的子包中。按照@sfat和Wim Deblauwe的建议进行更新。还是有同样的错误吗