如何解决" spring crudrepository bean not found"在春季启动?

时间:2018-01-05 14:09:21

标签: java spring spring-boot autowired

我是相对较新的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:Spring Tool Suite 版本:3.9.2.RELEASE Build Id: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>

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

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

我已经看过各种修补程序,说明了如何将我的应用程序移动到一个程序包中,但它已经在&#39; prs&#39;包。我还尝试过各种pom文件更改并重新安装了Eclipse(各种版本,但我目前正在使用上面提到的STS版本)。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:-1)

您需要将@Repository注释添加到ProductRepository;现在,你尝试自动安装一个不是bean的类,从而错误。