gpt4 book ai didi

java - 需要帮忙! Spring 启动错误: Unsatisfied DependancyException: Error creating bean with name ''

转载 作者:行者123 更新时间:2023-11-30 05:25:57 25 4
gpt4 key购买 nike

我无法弄清楚这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“restapiApplication”的bean时出错:通过方法“productRepository”参数0表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“productRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:无法为方法 public abstract com.murphy.demo.model.Product com.murphy.demo.repository.ProductRepository.findOne(java.lang.String) 创建查询!未找到 Product 类型的属性 findOne!

这是我的包中的存储库:


package com.murphy.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.murphy.demo.model.Product;


@Repository

public interface ProductRepository extends JpaRepository<Product,String> {

Product findOne(String id);


}


package com.murphy.demo.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.murphy.demo.model.Product;
import com.murphy.demo.repository.ProductRepository;

@RestController
@RequestMapping(path ="/api/products/" )
public class ProductsController {

private ProductRepository productRepository;


@Autowired
public void productRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}



@RequestMapping(path ="{id}", method = RequestMethod.GET)
public Product getProduct(@PathVariable(name = "id") String id) {

return productRepository.findOne(id);

}









}

包中的Product.java


package com.murphy.demo.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;



@Entity
public class Product {


@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator ="system-uuid")
@GenericGenerator(name ="system-uuid",strategy ="uuid")
private String id;
private String name;
private String description;
private String category;
private String type;


public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

}

主要应用:


package com.murphy.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.murphy.demo.model.Product;
import com.murphy.demo.repository.ProductRepository;



@SpringBootApplication

public class RestapiApplication implements CommandLineRunner{



private ProductRepository productRepository;


@Autowired
public void productRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}



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

@Override
public void run(String... args) throws Exception {
Product test = new Product();
test.setName("test");
test.setDescription("test");
test.setCategory("general");
test.setType("null");


productRepository.save(test);


}



}

Pom.xml:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.murphy.demo</groupId>
<artifactId>restapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restapi</name>
<description> project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>

</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Application.properites如下:


spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:file:~/Products;IFEXISTS=FALSE;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/h2-console
spring.jpa.show-sql= true

我已经尝试了我能想到的一切,任何建议都会有帮助。

谢谢!

最佳答案

通过将方法命名为“findOne”,Spring Data JPA 尝试创建一个查询来搜索产品类中的属性“findOne”,这不会不存在,因此出现错误“未找到 Product 类型的属性 findOne”。您应该指定您正在搜索的特性的名称。在您的情况下: findOneById (String id) 将创建一个查询以通过 Id 属性查找一个对象。

有关如何命名方法以创建正确查询的更多信息,您可以阅读 documentation关于这个话题。

关于java - 需要帮忙! Spring 启动错误: Unsatisfied DependancyException: Error creating bean with name '' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58607847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com