java - com.sparsh.conferencedemo.controllers.SessionController 中的 sessionRepository 需要一个找不到的 bean
问题描述
观看在线课程后,我试图创建一个简单的 CRUD spring boot 应用程序。但是当我运行该应用程序时,它显示一个错误“com.sparsh.conferencedemo.controllers.SpeakerController 中的字段 speakerRepository 需要一个找不到的 'com.sparsh.conferencedemo.repositories.SpeakerRepository' 类型的 bean。” 我不明白我做错了什么。我也是弹簧靴的新手。所以请提供任何帮助。我在下面发布我的代码。
这是我的会话控制器
package com.sparsh.conferencedemo.controllers;
import com.sparsh.conferencedemo.models.Session;
import com.sparsh.conferencedemo.repositories.SessionRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/sessions")
public class SessionController {
@Autowired
private SessionRepository sessionRepository;
@GetMapping
private List<Session> list(){
return sessionRepository.findAll();
}
@GetMapping
@RequestMapping("{id}")
private Session get(@PathVariable Long id){
return sessionRepository.getOne(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Session create(@RequestBody final Session session){
return sessionRepository.saveAndFlush(session);
}
@RequestMapping(value="{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable Long id){
sessionRepository.
}
@RequestMapping(value="{id}", method = RequestMethod.PUT)
public Session update(@PathVariable Long id, @RequestBody Session session){
Session existingSesseion=sessionRepository.getOne(id);
BeanUtils.copyProperties(session, existingSesseion, "session_id");
return sessionRepository.saveAndFlush(existingSesseion);
}
}
这是我的SessionRepository接口
package com.sparsh.conferencedemo.repositories;
import com.sparsh.conferencedemo.models.Session;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SessionRepository extends JpaRepository<Session, Long> {
}
这是我的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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sparsh</groupId>
<artifactId>conference-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>conference-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我的会话模型
package com.sparsh.conferencedemo.models;
import javax.persistence.*;
import java.util.List;
@Entity(name="sessions")
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long session_id;
private String session_name;
private String session_description;
private String session_length;
@ManyToMany
@JoinTable(
name="session_speakers",
joinColumns = @JoinColumn(name = "session_id"),
inverseJoinColumns = @JoinColumn(name = "speaker_id")
)
private List<Speaker> speakers;
public Session(){
}
public List<Speaker> getSpeakers() {
return speakers;
}
public void setSpeakers(List<Speaker> speakers) {
this.speakers = speakers;
}
public Long getSession_id() {
return session_id;
}
public void setSession_id(Long session_id) {
this.session_id = session_id;
}
public String getSession_name() {
return session_name;
}
public void setSession_name(String session_name) {
this.session_name = session_name;
}
public String getSession_description() {
return session_description;
}
public void setSession_description(String session_description) {
this.session_description = session_description;
}
public String getSession_length() {
return session_length;
}
public void setSession_length(String session_length) {
this.session_length = session_length;
}
}
这是我的主要应用程序类
package com.sparsh.conferencedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConferenceDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConferenceDemoApplication.class, args);
}
}
这些是我在 IntelliJ 中使用的模块,它是一个图像链接 https://imgur.com/a/riTdeot
这些是我在控制台中遇到的错误
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-29 12:44:29.844 INFO 10572 --- [ main] c.s.c.ConferenceDemoApplication : Starting ConferenceDemoApplication on DESKTOP-JBQF1EV with PID 10572 (C:\Users\spars\OneDrive\Desktop\conference-demo\target\classes started by spars in C:\Users\spars\OneDrive\Desktop\conference-demo)
2020-04-29 12:44:29.859 INFO 10572 --- [ main] c.s.c.ConferenceDemoApplication : No active profile set, falling back to default profiles: default
2020-04-29 12:44:38.081 INFO 10572 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-29 12:44:38.174 INFO 10572 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-29 12:44:38.175 INFO 10572 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-29 12:44:38.582 INFO 10572 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-29 12:44:38.582 INFO 10572 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 8561 ms
2020-04-29 12:44:39.083 WARN 10572 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionController': Unsatisfied dependency expressed through field 'sessionRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sparsh.conferencedemo.repositories.SessionRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-04-29 12:44:39.090 INFO 10572 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-04-29 12:44:39.647 INFO 10572 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-29 12:44:40.370 ERROR 10572 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field sessionRepository in com.sparsh.conferencedemo.controllers.SessionController required a bean of type 'com.sparsh.conferencedemo.repositories.SessionRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.sparsh.conferencedemo.repositories.SessionRepository' in your configuration.
Process finished with exit code 1
解决方案
看起来您遇到了与我在学习 Dan Bunker 的春季靴 Pluralsight 课程时遇到的相同问题?
看起来课程现在有点过时了,最新版本的 spring boot 似乎不适用于项目设置。具体来说,我认为这是我们如何定义 JpaRespositories 以使它们无法自动连接的问题。
显然,如果我们都在学习初学者课程,那么确定根本原因有点棘手。
我在 pom.xml 中将我的项目降级为 2.1.9.RELEASE:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它奏效了!祝你好运。
推荐阅读
- python - 在 Linux 中编写脚本
- android - 由于 http 信任管理器,应用程序被 Google Play 拒绝
- strapi - 有没有办法在strapi.io的文件上传中保留原始文件名?
- java - Guava 表到带有标题的 CSVPrinter
- powershell - 尽管尝试了 TFM 中的所有内容,PowerShell Select-String 仍不匹配
- javascript - 如何在 React 中使用 `||` 进行内联测试?
- routing - 用户按 F5 时如何更快地刷新组件
- php - Wordpress - CPT、ACF - 如何自定义管理员编辑帖子页面?
- r - 文件名作为 R 中小标题列表中的变量
- windows - 以编程方式为应用设置图形性能