【问题标题】:How to solve "spring crudrepository bean not found" in Spring boot?如何解决 Spring boot 中“找不到 spring crudrepository bean”?
【发布时间】:2018-01-05 14:09:21
【问题描述】:

我对 Spring 比较陌生,并且在工作机器上有一个工作项目,但我试图在我的个人笔记本电脑上设置相同的项目,并且在启动我的项目时遇到以下问题:

Description:
Field productRepository in prs.web.ProductController required a bean of type 'prs.domain.product.ProductRepository' that could not be found.
Action:
Consider defining a bean of type 'prs.domain.product.ProductRepository' in your configuration.

我已经在堆栈中进行了许多建议的修复,但都没有奏效。同样的项目在其他机器上也能正常工作,包括拉我项目的同行,所以我相信它在我的配置中。我正在使用以下内容: IDE:弹簧工具套件 版本:3.9.2.RELEASE 内部版本号:201712210947 平台:Eclipse Oxygen.2 (4.7.2) 操作系统:Windows 10

这是我的代码: POM 依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

应用类 - PRSWebApplication:

package prs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PRSWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(PRSWebApplication.class, args);
    }
}

实体类 - Product.java

package prs.domain.product;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int vendorID;
    private String partNumber;
    private String name;
    private double price;
    private String unit;
    private String photoPath;

    public Product() {
        id = 0;
        vendorID = 0;
        partNumber = "";
        name = "";
        price = 0.0;
        unit = "";
        photoPath = "";
    }

存储库:ProductRepository.java

package prs.domain.product;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface ProductRepository extends CrudRepository<Product, Integer>{

    List<Product> findAllByVendorID(int id);
    List<Product> findAllByVendorIDNot(int id);
}

控制器 - ProductController.java

package prs.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import prs.domain.product.Product;
import prs.domain.product.ProductRepository;
import prs.util.PRSMaintenanceReturn;

@Controller
@RequestMapping(path="/Products")
public class ProductController extends BaseController {
    @Autowired
    private ProductRepository productRepository;

    @GetMapping(path="/List")
    public @ResponseBody Iterable<Product> getAllProducts() {
        // This returns a JSON or XML with the users
        return productRepository.findAll();
    }

我已经看到了各种修复程序,这些修复程序说明了将我的应用程序移动到一个包中,但它已经在“prs”包中。我还尝试了各种 pom 文件更改并重新安装了 Eclipse(各种版本,但目前使用的是上面提到的 STS 版本)。

非常感谢任何帮助!

【问题讨论】:

  • 将@Repository注解添加到你的dao
  • 使用调试日志启动应用并检查实际的堆栈跟踪。
  • 您的数据库连接的配置在哪里?如果您用 groupid com.h2database artifact h2 替换 mysql 依赖项,您的代码工作正常,一切都开始了。我怀疑您的数据库连接不起作用,因此没有创建存储库,因此您遇到了自动装配问题。
  • @PaulNUK 如果是 mysql 配置错误或没有数据库可用的情况,那么错误将完全不同
  • 请在打开调试日志的情况下发布应用程序日志。

标签: java spring spring-boot autowired


【解决方案1】:

您需要将@Repository注解添加到ProductRepository;现在,您尝试自动装配一个不是 bean 的类,因此出现错误。

【讨论】:

  • 不,你不需要。存储库 bean 是通过 EnableJpaRepositories 注释创建的,它扫描正确类型的接口并创建一个 bean,它是所有存储库代码充实的接口的代理。
  • 不,它既不需要存储库也不需要组件 - 请参阅 Spring Data JPA 文档docs.spring.io/spring-data/jpa/docs/current/reference/html/…
  • 如果存储库没有自动装配,我会同意这一点。在我对 spring 的理解中,当你尝试自动装配一个 bean 时,你至少应该输入@Component
  • 那么 OP 也缺少 EnableJpaRepostories 注释
  • @Sikorski - 正如 M Denium 向我指出的那样,这不是必需的,因为 SpringBootApplication 会自动配置存储库初始化。
猜你喜欢
  • 2022-11-10
  • 2018-12-13
  • 2018-10-01
  • 1970-01-01
  • 2017-12-13
  • 2019-07-16
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多