Java 如何解决;“未找到春季积存豆”;春天的靴子?
我对Spring比较陌生,在工作机器上有一个工作项目,但我试图在我的个人笔记本电脑上设置相同的项目,在启动项目时遇到以下问题:Java 如何解决;“未找到春季积存豆”;春天的靴子?,java,spring,spring-boot,autowired,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
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工具套件
版本:3.9.2.1发布
构建Id:201712210947
平台:EclipseOxygen.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>
实体类-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);
}
包prs.domain.product;
导入java.util.List;
导入org.springframework.data.repository.crudepository;
公共接口ProductRepository扩展了Crudepository{
列出findAllByVendorID(int-id);
列出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();
}
package prs.web;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.ResponseBody;
导入prs.domain.product.product;
导入prs.domain.product.ProductRepository;
导入prs.util.PRSMaintenanceReturn;
@控制器
@请求映射(路径=“/Products”)
公共类ProductController扩展了BaseController{
@自动连线
私有产品存储库产品存储库;
@GetMapping(path=“/List”)
public@ResponseBody Iterable getAllProducts(){
//这将返回用户的JSON或XML
返回productRepository.findAll();
}
我看到过各种修复程序,说明了如何将我的应用程序移动到一个包中,但它已经在“prs”包中。我还尝试了各种pom文件更改,并重新安装了Eclipse(各种版本,但目前正在使用上面提到的STS版本)
非常感谢您的帮助!您需要将
@Repository
注释添加到ProductRepository
;现在,您尝试自动连接一个不是bean的类,因此会出现错误。将@Repository注释添加到DAO中使用调试日志启动应用程序并检查实际堆栈跟踪。您的d的配置在哪里数据库连接?如果您用groupid com.h2数据库工件h2替换mysql依赖项,您的代码工作正常,一切都启动了。我怀疑您的数据库连接不工作,因此无法创建存储库,因此您存在自动连接问题。@PaulNUK如果是mysql配置错误或没有可用数据库的话如果出现这种情况,则错误将完全不同。请在启用调试日志的情况下发布应用程序日志。不,您不需要这样做。存储库bean是通过EnableJpaRepositories注释创建的,该注释扫描正确类型的接口,并创建一个bean,该bean是该接口与所有存储库的代理代码充实。不,它不需要存储库或组件-请参阅Spring数据JPA文档。如果存储库不是自动连接的,我会同意这一点。在我对Spring的理解中,当您尝试自动连接bean时,您至少应该放置@component
。OP缺少EnableJpaRepostories注释also@Sikorski-作为采购订单M Denium告诉我,这是不需要的,因为SpringBootApplication自动配置存储库初始化。
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();
}