gpt4 book ai didi

java - HomePageController 中的字段 itemRepository 需要一个 ItemRepository 类型的 bean,但无法找到

转载 作者:行者123 更新时间:2023-11-30 06:06:32 27 4
gpt4 key购买 nike

我是 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.

这是 pom.xml

<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>

my classes and hierarchy

应用程序类:

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. 将 spring-data-jpa 更改为 spring-boot-starter-data-jpa 并删除 hibernate 依赖项。它带有启动器。
  2. 在主类中使用@SpringBootApplication。

关于java - HomePageController 中的字段 itemRepository 需要一个 ItemRepository 类型的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51205766/

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