Search code examples
springspring-bootrepositoryjavabeans

No qualifying bean of type 'tn.esen.dao.StudentRepository' available


i'm developing a crud application with SPRING and as usual, I faced a problem saying that no qualifying beans is available and didn't even add the student table in the database.

this is the problem:

Exception in thread "main" org.spring framework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'tn.esen.dao.StudentRepository' available

entity:

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
    }
}

controller:

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");
        
    }
}

repository:

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> {
    
}

main program:

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); 
         }
    }
}

Solution

  • Your StudentRepository is in a complete different package: tn.esen.dao You should add @ComponentScan(basePackages = "tn.esen") to your DemoApplication.java