我正在用spring开发一个crud应用程序,像往常一样,我遇到了一个问题:没有合格的bean可用,甚至没有在数据库中添加student表。
这就是问题所在:
线程“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);
}
}
}
1条答案
按热度按时间qacovj5a1#
你的
StudentRepository
在一个完全不同的包中:tn.esen.dao
你应该加上@ComponentScan(basePackages = "tn.esen")
给你的DemoApplication.java