I am new to Springboot and currently trying to recreate the example from Baeldung where a simple fullstack project is built using React as front end (). From what I've gathered so far, when creating a repository using the JpaRepository interface, Spring basically creates a class in the background and I can then call the methods from JpaRepository.
However, after attempting this on my own, I get this error when I try to start my spring boot application: No qualifying bean of type 'com.example.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I used a class implementation in the end to make the application run. This is my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.6</version>
</dependency>
My repository interface:
package com.example.repository;
import com.example.model.Client;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
public interface ClientRepository extends JpaRepository<Client, Long> {
}
Controller class:
@RestController
@RequestMapping("/clients")
@EnableJpaRepositories
@SpringBootApplication(scanBasePackages = {"com.example","com.example.repository"})
public class ClientsController {
private final ClientRepository clientRepository;
public ClientsController(ClientRepository clientRepository){
this.clientRepository = clientRepository;
}
*** Remaining API methods ***
I have has to use a class implementation of JpaRepository to get enable the Springboot application to start. My question is: do I have to go on and fully implement my own data access layer since the default JpaRepository does not seem to be enough?
CodePudding user response:
You have to add @Repository
annotation for SpringBoot to create the Bean
CodePudding user response:
You have to remove the following from the ClientsController
@EnableJpaRepositories
@SpringBootApplication(scanBasePackages = {"com.example","com.example.repository"})
And make sure that you add @SpringBootApplication
to entry point of application.