【问题标题】:Need Help! Spring Boot error: Unsatisfied DependancyException: Error creating bean with name ''需要帮忙! Spring Boot 错误:Unsatisfied DependancyException:创建名称为“”的 bean 时出错
【发布时间】:2020-02-24 17:13:48
【问题描述】:

我无法弄清楚这个错误:

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

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

谢谢!

【问题讨论】:

  • 我认为您的意思是将 ProductController 中的 @Autowired 注释应用于 ProductsController 构造函数。您已将其应用于名为 productRepository() 的函数 try @Autowired public void ProductsController(ProductRepository productRepository) { this.productRepository = productRepository; } 与 restapiapplication 相同
  • 删除findOne 方法。 Spring Data 包含一个findById 方法,在早期版本中被命名为findOne。

标签: java spring hibernate maven spring-boot


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    为什么不使用 CrudRepository 而不是 JpaRepository? findOne(id) 将自动可用。这样一来,您的存储库界面就应该是空的。

    https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

    【讨论】:

    • JpaRepository 不也是CrudRepository 吗?由于@Paschoal 解释的原因,我认为错误仍然会发生
    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 2019-04-27
    • 2023-02-09
    • 2020-01-24
    • 2017-04-17
    • 2019-02-12
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多