springhibernaterepositoryspring-data-jpaspring-repositories

Spring data repository not found at compile time


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


Solution

  • Thank to @JBNizet for his comment, that made it working.

    I create this answer since he did not:

    Replace SpringApplication.run(SampleController.class, args); with SpringApplication.run(InvestStoreApplication.class, args);. And remove the useless @EnableAutoConfiguration on your controller.