在编译时找不到Spring数据存储库

问题描述 投票:1回答:2

我试图在Spring Boot应用程序中使用Spring数据和存储库,但编译项目时出错。

这是我的实体:

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;

    ...
}

和相应的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> {

}

我在WS控制器中使用它,通过Autowired注释生成存储库:

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);
    }
}

但是在编译项目时,我得到:


应用程序未能启动


描述:fr.investstore.ws.SampleController中的字段crowdOperationRepository需要一个无法找到的类型为'fr.investstore.repositories.CrowdOperationRepository'的bean。

操作:考虑在配置中定义类型为“fr.investstore.repositories.CrowdOperationRepository”的bean。

Woud not Spring是否通过接口自动为存储库生成bean?我该如何解决这个问题?

编辑:我也尝试将Repository注释(从org.springframework.stereotype.Repository)放到CrowdOperationRepository,但我得到了同样的错误

spring hibernate repository spring-data-jpa spring-repositories
2个回答
3
投票

在创建spring-boot应用程序时,我们需要在脑海中保持一些点

  1. 始终将主类(带有`@SpringBootApplication注释的类)保留在顶级包中,其他类应位于子包下。
  2. 始终使用适当的注释标记您的bean类,例如所有存储库都应该用@Repository注释标记,所有服务实现类都应该用@Service标记,其他组件类应该用@Component标记,定义我们bean的类应该标记为@Configuration
  3. 启用您正在使用的功能,例如@EnableJpaRepositories@EnableTransactionManagement@EnableJpaAuditing,这些注释还提供了允许我们定义哪个包弹簧需要扫描的功能。

所以在你的情况下,你需要用InvestStoreApplication注释标记@EnableJpaRepositories类,用CrowdOperationRepository标记@Repository


1
投票

你必须告诉你的spring启动应用程序加载JPA存储库。

将此副本复制到您的应用程序类

它将自动扫描您的JPA存储库并将其加载到您的spring容器中,即使您没有使用@Repository定义您的接口,它也会在您的依赖类中连接该bean。

@EnableJpaRepositories(basePackages = { "fr.investstore.repositories" })
© www.soinside.com 2019 - 2024. All rights reserved.