【发布时间】:2021-02-06 10:50:27
【问题描述】:
我正在使用 SPRING 开发一个 crud 应用程序,像往常一样,我遇到了一个问题,说没有可用的合格 bean,甚至没有在数据库中添加学生表。
这就是问题所在:
线程“main”org.spring framework.beans.factory.NoSuchBeanDefinitionException 中的异常:没有“tn.esen.dao.StudentRepository”类型的合格 bean 可用
实体:
package tn.esen.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
@Table(name="etud")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String nom;
private String prenom;
private Double moyenne;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public Double getMoyenne() {
return moyenne;
}
public void setMoyenne(Double moyenne) {
this.moyenne = moyenne;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
控制器:
package tn.esen.control;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tn.esen.dao.StudentRepository;
import tn.esen.entity.Student;
@Controller
public class MycontrolStudent {
@Autowired
public StudentRepository metier;
@RequestMapping(value="/index")
public String home(Model mymodel) {
//importing the list of all students
List<Student> allstudents= metier.findAll();
//adding an attribute to the model contain the list of students
mymodel.addAttribute("listestudent",allstudents);
return ("acceuil");
}
}
存储库:
package tn.esen.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import tn.esen.entity.Student;
@Repository
public interface StudentRepository extends JpaRepository<Student,Long> {
}
主程序:
package com.example.demo;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import tn.esen.dao.StudentRepository;
import tn.esen.entity.Student;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
StudentRepository StudentRepository = ctx.getBean(StudentRepository.class);
List<Student> allstud = StudentRepository.findAll();
System.out.println("--------------------list of students-----------------------");
for(Student stud:allstud) {
System.out.println(stud);
}
}
}
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
标签: spring spring-boot repository javabeans