【发布时间】: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