Я пытался создать простое приложение для весенней загрузки CRUD после просмотра онлайн-курса. Но когда я запустил приложение, оно показало ошибку «Поле speakerRepository в com.spar sh .conferencedemo.controllers.SpeakerController требуется компонент типа 'com.spar sh .conferencedemo.repositories.SpeakerRepository', который не может быть найден «. Я не понимаю, что я делаю не так. Также я новичок в весеннем ботинке. Так что любая помощь, пожалуйста. Я публикую свой код ниже.
Это мой SessionController
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 file
<?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