当前位置: 首页 > 知识库问答 >
问题:

HomePageController中的字段itemRepository需要的类型为itemRepository的bean找不到

卢嘉誉
2023-03-14

我是spring boot的新手,一直在尝试制作简单的REST web应用程序。我遵循了不同指南中所示的步骤,但当我尝试运行应用程序时,仍然会遇到同样的问题。

这是我每次尝试运行代码时看到的输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.3.RELEASE)

2018-07-06 10:47:44.768  INFO 11172 --- [           main] App.App                                  : Starting App on DESKTOP-OTCRH25 with PID 11172 (started by EhabArman-Notebook in E:\java workplace\eclipse-workplace\full)
2018-07-06 10:47:44.784  INFO 11172 --- [           main] App.App                                  : No active profile set, falling back to default profiles: default
2018-07-06 10:47:44.925  INFO 11172 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@75c072cb: startup date [Fri Jul 06 10:47:44 EEST 2018]; root of context hierarchy
2018-07-06 10:47:46.785  INFO 11172 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-07-06 10:47:46.816  INFO 11172 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-07-06 10:47:46.816  INFO 11172 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-06 10:47:46.840  INFO 11172 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_171\bin;C:\Program Files\Apache Software Foundation\apache-maven-3.5.4\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PostgreSQL\10\lib;C:\Program Files\PostgreSQL\10\bin;C:\Program Files\apache-log4j-2.11.0-bin\log4j-api-2.11.0.jar;C:\Program Files\apache-log4j-2.11.0-bin\log4j-corei-2.11.0.jar;C:\Program Files\Git\cmd;C:\Program Files\MySQL\MySQL Shell 8.0\bin;C:\Program Files\MySQL\MySQL Server 8.0\bin;;C:\Program Files\Microsoft VS Code\bin;C:\Users\EhabArman-Notebook\Desktop;;.]
2018-07-06 10:47:46.985  INFO 11172 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-07-06 10:47:46.985  INFO 11172 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2060 ms
2018-07-06 10:47:47.172  INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-07-06 10:47:47.177  INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-06 10:47:47.178  INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-06 10:47:47.179  INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-06 10:47:47.179  INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-06 10:47:47.221  WARN 11172 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homePage': Unsatisfied dependency expressed through field 'itemRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'App.components.repository.ItemRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-07-06 10:47:47.224  INFO 11172 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-07-06 10:47:47.247  INFO 11172 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-06 10:47:47.390 ERROR 11172 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field itemRepository in App.components.controller.HomePage required a bean of type 'App.components.repository.ItemRepository' that could not be found.


Action:

Consider defining a bean of type 'App.components.repository.ItemRepository' in your configuration.
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Test</groupId>
  <artifactId>Full</artifactId>
  <version>0.0.1-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>


  <dependencies>


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


        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            </dependency>




    </dependencies>

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

</project>
package App;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication
public class App {

    public static void main(String[] args) {

        SpringApplication.run(App.class, args);
    }
}



package App.components.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import App.components.repository.*;
import App.components.repository.*;


@Controller
public class HomePage {

    @Autowired
    private ItemRepository itemRepository;


    @RequestMapping("/homepage")
    public String getHomePage() {   
        return "homepage/homepage.html";
    }


}
package App.components.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import App.components.model.Item;

@Repository
public interface ItemRepository extends CrudRepository<Item, Long>{

}

项目类别:

package App.components.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;



@Entity
@Table(name = "item")
public class Item {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "quantity")
    private int quantity;

    @Column(name = "price")
    private int price;


    public Item() {}

    public Item(String name,int quantity ,int price) {
           this.name = name;
           this.quantity = quantity;
           this.price = price;
       }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString() {
        return "[" + id + " " + name + " " + price + " " + quantity +"]";
    }

}

共有1个答案

李兴为
2023-03-14
  1. 将spring-data-jpa更改为spring-boot-starter-data-jpa,并删除hibernate依赖项。它带有启动程序。
  2. 在main类中使用@SpringBootApplication。
 类似资料:
  • 我是一名spring boot学习者,所以我一直在尝试创建一些基本的spring boot应用程序。我试图运行开发的应用程序时出错。 我的错误是[[https://i.stack.imgur.com/oyQDi.png][1]][1] java: ItemDetails.java:[软件包名称:io.ajithan.springbootstarter.model] ItemResponse.jav

  • 结构没有问题。spring boot可以扫描UserMapper,但不能扫描UserService。我试着给我的UserService@Mapper组件,然后它就可以被扫描了。但我不知道如何使用其他方法进行扫描。我尝试了@服务,但不起作用。

  • 我是Spring新来的,所以我有一个这样的问题:描述: 字段templateEngine在com.diet4you。拉普科·叶卡捷琳娜。MailComponent需要找不到类型org.thymeleaf.TemplateEngine的bean。 注入点具有以下注释:-@org。springframework。豆。工厂注释。自动连线(必需=真) 已找到以下候选项,但无法注入:-“ThymeleafA

  • 问题内容: 每当启动应用程序spring启动时,我都会收到以下错误。 申请开始失败 描述: com.base.model.AbstractDao中的现场会话需要找不到“ org.hibernate.SessionFactory”类型的Bean。 行动: 考虑在配置中定义类型为“ org.hibernate.SessionFactory”的bean。 我添加了我的应用程序的实现: POM.xml 应

  • 我有一个java项目,它将Spring Boot与JPA结合使用,并将Hibernate用于数据库。我正在尝试建立一个访问数据库的微服务。(我不熟悉微服务和Spring Boot)。 以下是主要课程: IGmCircularsDAO. class: GMCircularsDAOImpl。类别: ParentDAO。班 循环服务。班 当我运行这段代码时,我遇到了以下错误,我已经陷入其中一段时间了。

  • 有人知道我怎么解决这个问题吗?请告诉我,谢谢。