muhammad bouabid
muhammad bouabid

Reputation: 21

Required a bean of type 'com.example.dao.InterfaceName' that could not be found

I'm using Eclipse and maven with Java 1.8 trying to build a spring started project, which is based on maven project so I build my own entity Candidat with this full code block

package com.example.demo.entities;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name = "CandidatTable")
public class Candidat implements Serializable {
    
    @Id @GeneratedValue
    private Long id;

    private String name;
    private String prenom;
    private String reference;
    private String resumeCandidate;

    public Candidat() {
        super();
    }

    public Candidat(String name, String prenom, String reference, String resumeCandidate) {
        super();
        this.name = name;
        this.prenom = prenom;
        this.reference = reference;
        this.resumeCandidate = resumeCandidate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getResumeCandidate() {
        return resumeCandidate;
    }

    public void setResumeCandidate(String resumeCandidate) {
        this.resumeCandidate = resumeCandidate;
    }

}

in normal case we should build an Interface into it we should define our services method like we said : save(), findAllRecords(),findSpecificRecordByID(),updateRecordLine(),deleteRecord() ...etc, but in my case I used Spring-data in my maven project that in my web.xml file I have this dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

so there's no need to define our methods, because Spring-data use its own methods in situation we create an interface which extends the Generic Interface JpaRepository, so my interface look like this:

package com.example.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entities.Candidat;

public interface ICandidate extends JpaRepository<Candidat, Long>{
//no need to define our methods, because we gonna use methods defined 
// by SpringData whose comes from JPA specification.
}

Finally, the main class code is this one :

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 com.example.dao.ICandidate;
import com.example.demo.entities.Candidat;

@SpringBootApplication
public class CatWebServiceApplication {

    
    public static void main(String[] args) {
        ApplicationContext context = 
                SpringApplication.run(CatWebServiceApplication.class, args);
        ICandidate condidateRep = context.getBean(ICandidate.class);
        
        condidateRep.save(
                new Candidat("UserName_1", "FirstName_1", "Ref_1", "/Home/file/docFile_1.docx")); //insert data using Ioc later after runing urself

        condidateRep.save(
                new Candidat("UserName_2", "FirstName_2", "Ref_2", "/Home/file/docFile_2.docx"));
        
        List<Candidat> cnds = condidateRep.findAll();
        cnds.forEach(p-> System.out.println(p.getResumeCandidate()));
    }
}
 

My application SHOULD turn well, look to web.xml to manages the application dependcies, then its look into the Path src/main/resources which contain the file application.properties that contain this code:

# DataSource settings:
spring.datasource.url = jdbc:mysql://localhost:3306/softherWebService
spring.datasource.username = root
spring.datasource.password = dbPassword
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Suddenly I get this error message on my eclipse console:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.dao.ICandidate' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at com.example.demo.CatWebServiceApplication.main(CatWebServiceApplication.java:19)

so the problem in getBean() line, I used the CommandLineRunner too but doesn't solve my problem in getting the bean.

Upvotes: 1

Views: 1894

Answers (2)

jalil
jalil

Reputation: 505

Add @Repository annotation above your interface definition to get ride of the error.

Upvotes: 0

M. Deinum
M. Deinum

Reputation: 124546

When using @SpringBootApplication that automatically implies a @ComponentScan. The default behavior for @ComponentScan is, when not explicitly having basePackages defined, is to start scanning from the package from the class it has been defined on. For Spring Boot this also applies to all other auto-configuration like detecting entities, detecting Spring Data repositories etc.

Now as your CatWebServiceApplication is defined in the com.example.demo and your ICandidate in com.example.dao the latter will not be scanned because it isn't part of the com.example.demo package.

There are several ways to fix this.

First you could specify the scanBasePackages on the @SpringBootApplication to have components detected, however that wouldn't solve this as you would also need @EnableJpaRepositories("com.example.dao") and @EntityScan("com.example.dao") and probably a couple more when extending the technologies.

The easiest, and recommended way, is to put your CatWebServiceApplication in the com.example package so that all sub-packages are covered and you don't need to think about all the additional annotations you would need to add.

Upvotes: 0

Related Questions