I am trying to use Spring data and repositories in a Spring Boot application, but I have an error when compiling the project.
Here is my Entity :
package fr.investstore.model;
import javax.persistence.Id;
...
@Entity
public class CrowdOperation {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
@Enumerated(EnumType.STRING)
public RepaymentType repaymentType;
...
}
And the corresponding Repository:
package fr.investstore.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import fr.investstore.model.CrowdOperation;
public interface CrowdOperationRepository extends CrudRepository<CrowdOperation, Long> {
}
I use it in a WS controller, generating a repository through the Autowired annotation:
package fr.investstore.ws;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
...
@Controller
@EnableAutoConfiguration
public class SampleController {
@Autowired
private CrowdOperationRepository crowdOperationRepository;
@RequestMapping(path = "/", method = RequestMethod.GET)
@ResponseBody
public String getOperations(@RequestParam(required=true, defaultValue="Stranger") String name) {
crowdOperationRepository.save(new CrowdOperation());
return "Hello " + name;
}
}
And the code of the application:
package fr.investstore;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import fr.investstore.ws.SampleController;
@SpringBootApplication
public class InvestStoreApplication {
public static void main(String[] args) {
SpringApplication.run(SampleController.class, args);
}
}
But when compiling the project I get:
APPLICATION FAILED TO START
Description: Field crowdOperationRepository in fr.investstore.ws.SampleController required a bean of type 'fr.investstore.repositories.CrowdOperationRepository' that could not be found.
Action: Consider defining a bean of type 'fr.investstore.repositories.CrowdOperationRepository' in your configuration.
Woudn't Spring automatically generate a bean for the repository through the interface? How can I resolve this?
EDIT: I also tried to put the Repository annotation (from org.springframework.stereotype.Repository) onto CrowdOperationRepository, but I got the same error
While creating a spring-boot application, we need to keep some point in our mind like
Always keep main class (class with `@SpringBootApplication annotation) on the top level package and other classes should lie under sub-packages.
Always mark your bean classes with proper annotation e.g. all repositories should be marked by @Repository annotation, all service implementation classes should be marked with @Service, other component classes should be marked by @Component, class which defines our beans should be marked as @Configuration
Enable the feature which you are using e.g. @EnableJpaRepositories, @EnableTransactionManagement, @EnableJpaAuditing, these annotations also provides functionality which let us define which package spring needs to scan.
So in your case, you need to mark InvestStoreApplication class with @EnableJpaRepositories annotation and CrowdOperationRepository with @Repository.
you have to tell your spring boot application to load JPA repositories.
copy this one to your application class
it will auto-scan your JPA repository and load it in your spring container even if you do not define your interface with @Repository it will wire that bean in your dependent class.
@EnableJpaRepositories(basePackages = { "fr.investstore.repositories" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With