Search code examples
javaspringjavabeans

Field xx in xx service required a bean of type


I'm getting the next error :

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

I have a controller that has 4 services with autowired annotation :

@RestController
public class Controller {


@Autowired
private AnsService ansService

@Autowired
private QService QService;

@Autowired
private UService UService;

Each of those services has the @Service annotation :

@Service
public class ansService {

@Autowired
private AnsRepo ansrepo;

Each repository also has a @Repository annotation :

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

The error appears for the first autowired object in the controller, so it seems that the spring application can't find the autowired classes..

The structure of my project :

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

My main :

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

I'm pretty sure that the issue is with the @SpringBootApplication annotation.I saw that there are many posts about this error but in my case I implemented already all the solutions that were provided.

What else should I check ?

Update

I added in my Main app the @EnableJpaRepositories("com.myproject.repositories") annotation. Now I got the next error :

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

The dependencies in my pom.xml file :

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

Why do I need to mention the @EnableJPARepostiory annotation when I have the @SpringBootApplication? And why I'm getting that error?


Solution

  • The solution in my case was just adding the next dependency in the pom.xml :

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

    instead of :

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