需要一个无法找到的'com.example.dao.InterfaceName'类型的bean

问题描述 投票:1回答:2

我正在使用Eclipse和maven与Java 1.8尝试构建一个基于maven项目的spring启动项目,所以我用这个完整的代码块构建我自己的Entity underName Candidat

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

}

在正常情况下,我们应该构建一个接口,我们应该定义我们的服务方法,如我们所说:save()findAllRecords()findSpecificRecordByID()updateRecordLine()deleteRecord() ......等等,但在我的情况下,我在我的maven项目中使用Spring-data,在我的web.xml文件我有这个依赖:

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

所以没有必要定义我们的方法,因为Spring-data使用自己的方法我们创建了一个扩展Generic Interface JpaRepository的接口,所以我的界面如下所示:

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 difined 
// by SpringData whose comes from JPA specification.
}

最后,主类代码是这样的:

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

我的应用程序应该转好,查看web.xml来管理应用程序依赖,然后查看包含包含此代码的文件src/main/resources的Path application.properties

# 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

突然,我在我的eclipse控制台上收到此错误消息:

线程“main”中的异常org.springframework.beans.factory.NoSuchBeanDefinitionException:在org.springframework的org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)中没有类型为'com.example.dao.ICandidate'的限定bean位于com.example.demo.CatWebServiceApplication.main(CatWebServiceApplication.java)的org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)中的.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) :19)

所以在getBean()行中的问题,我也使用了CommandLineRunner但是没有解决我在获取bean时遇到的问题。

maven spring-boot spring-data spring-data-jpa
2个回答
0
投票

当使用自动暗示@SpringBootApplication@ComponentScan时。当未明确定义@ComponentScan时,basePackages的默认行为是从已定义的类开始从包中扫描。对于Spring Boot,这也适用于所有其他自动配置,如检测实体,检测Spring Data存储库等。

现在你的CatWebServiceApplication定义在com.example.demo和你的ICandidatecom.example.dao,后者将不会被扫描,因为它不是com.example.demo包的一部分。

有几种方法可以解决这个问题。

首先,您可以在scanBasePackages上指定@SpringBootApplication以检测组件,但是这不会解决这个问题,因为您还需要@EnableJpaRepositories("com.example.dao")@EntityScan("com.example.dao"),并且在扩展技术时可能需要更多。

最简单,最推荐的方法是将CatWebServiceApplication放在com.example包中,以便覆盖所有子包,并且您不需要考虑需要添加的所有其他注释。


0
投票

在界面定义上方添加@Repository注释以获取错误。

© www.soinside.com 2019 - 2024. All rights reserved.