0

I'm getting the following error when using autowire in spring boot

***************************
APPLICATION FAILED TO START
***************************

Description:

Field candidateRepository in io.xgeek.interviewercalendarapi.service.CandidateServiceImpl required a bean of type 'io.xgeek.interviewercalendarapi.repository.CandidateRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

The autowire, is from the ServiceImpl class, who calls the controller

@Service
public class CandidateServiceImpl implements  CandidateService{

    @Autowired
    private CandidateRepository candidateRepository;


   // ... interface methods here...

My repository interface:

@Repository
public interface CandidateRepository extends JpaRepository <Candidate, Long> {
}

Service interface:

public interface CandidateService {

    Candidate saveCandidate(Candidate candidate);
    List<Candidate> fetchCandidateList();
}

`

Main class: `

@SpringBootApplication
public class InterviewerCalendarApiApplication {

   public static void main(String[] args) {
      SpringApplication.run(InterviewerCalendarApiApplication.class, args);
   }

}

My pom.xml dependecies:

<dependencies>

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
   </dependency>
   <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>runtime</scope>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
   



   <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.24</version>
      <scope>provided</scope>
   </dependency>
</dependencies>

My project structure

I've already try everything. The classes surely are in the correct order, they're inside the packages who are child of the main of the spring boot application. @ComponentScan the packages didn't work.Scan packages didn't work. All the notations appear to be right. I've searched to every questions of this type and I couldn't find a solution.

  • Could you please update your question with the methods in `CandidateRepository`? – rakesh Dec 01 '22 at 16:13
  • I'm not implementing any method in CandidateRepository – Arthur Konrad Dec 01 '22 at 16:18
  • I think this is similar to this question. Could you please check? https://stackoverflow.com/questions/29221645/cant-autowire-repository-annotated-interface-in-spring-boot – rakesh Dec 01 '22 at 16:28
  • As I said, I've tried all solutions in this post and similar. @EnableJpaRepositories("") @EntityScan("") didn't work for me. – Arthur Konrad Dec 01 '22 at 16:29

1 Answers1

0

I haven't worked with Spring Data for quite a while, but the web suggests that you need to modify your service class to include the @PersistenceContext annotation.

@Service
public class CandidateServiceImpl implements  CandidateService {

    @PersistenceContext
    private EntityManager entityManager;

...

An example: How to get JPA EntityManager in Spring Boot

pfurbacher
  • 1,789
  • 3
  • 15
  • 23