如何解决没有可用的“tn.esen.dao.StudentRepository”类型的合格 bean
我正在使用 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);
}
}
}
解决方法
您的 StudentRepository
在一个完全不同的包裹中:tn.esen.dao
您应该添加 @ComponentScan(basePackages = "tn.esen")
到您的DemoApplication.java
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。