xx 服务中的字段 xx 需要一个类型的 bean

Field xx in xx service required a bean of type

我遇到下一个错误:

Field ansRepo in com.myproject.services.ansService required a bean of type 'com.myproject.repositories.ansRepo' that could not be found.

我有一个控制器,它有 4 个带有自动装配注释的服务:

@RestController
public class Controller {


@Autowired
private AnsService ansService

@Autowired
private QService QService;

@Autowired
private UService UService;

这些服务中的每一个都有@Service 注释:

@Service
public class ansService {

@Autowired
private AnsRepo ansrepo;

每个存储库还有一个@Repository 注释:

@Repository
public interface AnsRepo extends JpaRepository<Ans,Long> {
...
}

控制器中的第一个自动装配对象出现错误,因此 spring 应用程序似乎无法找到自动装配的 类..

我的项目结构:

-com.myproject
----Main.java
----controllers
------------Controller.java
----entities
------------ans.java
----repositories
------------ansRepo.java
----services
------------ansService.java

我的主要 :

package com.postmalone.Application;

import org.springframework.boot.SpringApplication;

 @SpringBootApplication(scanBasePackages={"com.myproject.controllers", 
 "com.myproject.services", "com.myproject.repositories", 
 "com.myproject.entities"})

 public class Main {
 public static void main(String[] args) throws InterruptedException {
    SpringApplication.run(Main.class,args);

我很确定问题出在@SpringBootApplication annotation.I 看到有很多关于这个错误的帖子,但就我而言,我已经实施了提供的所有解决方案。

知道我还应该检查什么吗?

----更新---

我在主应用程序中添加了 @EnableJpaRepositories("com.myproject.repositories") 注释。现在我得到了下一个错误:

Field ansRepo in com.postmalone.services.AnswService required a bean named 'entityManagerFactory' that could not be found.

我的 pom.xml 文件中的依赖项:

<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.1.4</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

</dependencies>

知道为什么我在有 @SpringBootApplication 时需要提及 @EnableJPARepostiory 注释吗?为什么我会收到这个错误?

好的,我注意到您没有应该为控制器、实体、存储库和服务共享的根包。 为了在根包的开头进行扫描,只需通过以下方式添加:

 @SpringBootApplication(scanBasePackages = "com.myproject")

并按照以下方式重命名其他包:

 com.myproject 
    >   Main.java
 com.myproject.controllers
    >   Controller.java
 com.myproject.entities
    >   Ans.java
 com.myproject.repositories
    >   AnsRepo.java
 com.myproject.services
    >   AnsService.java

我的解决方案只是在 pom.xml 中添加下一个依赖项:

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

而不是:

    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.3.6.Final</version>
</dependency>