我正在学习Spring,一切进展顺利,但是突然遇到无法找到合格bean的问题。碰壁,即使在新应用中,我也能做到。

让我知道是否需要更多,请稍候!我一定会错过一些非常简单的东西。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
  No qualifying bean of type [com.alco.repository.ContactRepository]
  found for dependency [com.alco.repository.ContactRepository]:
  expected at least 1 bean which qualifies as autowire candidate for this dependency.
  Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


我的依赖:

<dependencies>
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>


联系人类别:

package com.example.entity;

import java.io.Serializable;

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

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Contact implements Serializable {

private static final long serialVersionUID = -1340978779095092824L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private String id;
private String firstName;
private String lastName;
private String address;
private String phoneNumber;
private String email;

}


简单的界面:

package com.alco.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.alco.entity.Contact;

public interface ContactRepository extends JpaRepository<Contact, String> {

}

最佳答案

我认为您缺少启用JPA存储库的功能:

@ComponentScan(basePackageClasses = ...)
@EntityScan(basePackageClasses = ...)
@EnableAutoConfiguration
@EnableJpaRepositories(basePackageClasses = ...)
public class ... {
}


这将用于您的配置类。

09-25 20:36