在编译时找不到Spring数据存储库 [英] Spring data repository not found at compile time
问题描述
我正在尝试在Spring Boot应用程序中使用Spring数据和存储库,但是在编译项目时出现错误.
I am trying to use Spring data and repositories in a Spring Boot application, but I have an error when compiling the project.
这是我的实体:
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;
...
}
以及相应的存储库:
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> {
}
我在WS控制器中使用它,通过Autowired
批注生成了一个存储库:
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;
}
}
以及应用程序代码:
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
描述:现场人群 fr.investstore.ws.SampleController需要一个类型为bean的bean 无法提供的"fr.investstore.repositories.CrowdOperationRepository" 被发现.
Description: Field crowdOperationRepository in fr.investstore.ws.SampleController required a bean of type 'fr.investstore.repositories.CrowdOperationRepository' that could not be found.
操作:考虑定义一个类型的bean 您的"fr.investstore.repositories.CrowdOperationRepository" 配置.
Action: Consider defining a bean of type 'fr.investstore.repositories.CrowdOperationRepository' in your configuration.
Spring是否不会通过界面为存储库自动生成一个bean? 我该如何解决?
Woudn't Spring automatically generate a bean for the repository through the interface? How can I resolve this?
我也尝试将Repository
注释(来自org.springframework.stereotype.Repository
)放到CrowdOperationRepository
上,但是出现了相同的错误
I also tried to put the Repository
annotation (from org.springframework.stereotype.Repository
) onto CrowdOperationRepository
, but I got the same error
推荐答案
在创建Spring-boot应用程序时,我们需要牢记一些重点,例如
While creating a spring-boot application, we need to keep some point in our mind like
-
始终将主类(带有@SpringBootApplication批注的类)保留在顶级程序包中,而其他类应位于子程序包下.
Always keep main class (class with `@SpringBootApplication annotation) on the top level package and other classes should lie under sub-packages.
总是用适当的注释标记您的bean类,例如所有存储库均应标有@Repository
注释,所有服务实现类应标有@Service
,其他组件类应标有@Component
,定义我们的bean的类应标为@Configuration
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
启用您正在使用的功能,例如@EnableJpaRepositories
,@EnableTransactionManagement
,@EnableJpaAuditing
,这些注释还提供了使我们能够定义spring需要扫描的软件包的功能.
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.
因此,在您的情况下,您需要使用@EnableJpaRepositories
注释标记InvestStoreApplication
类,并使用@Repository
标记CrowdOperationRepository
.
So in your case, you need to mark InvestStoreApplication
class with @EnableJpaRepositories
annotation and CrowdOperationRepository
with @Repository
.
这篇关于在编译时找不到Spring数据存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!