简体   繁体   English

Spring Boot JPA-没有合格的bean类型

[英]Spring Boot JPA - No qualifying bean of type

I'm learning Spring and things were going well but suddenly running into this issue where it cannot find a qualified bean. 我正在学习Spring,一切进展顺利,但突然遇到无法找到合格bean的问题。 Hitting the wall, even in a new app I'm getting this. 碰壁,即使在新应用中,我也能做到。

Let me know if you need more, going to take a break! 让我知道是否需要更多,请稍候! I must be missing something very simple. 我一定会错过一些非常简单的东西。

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

My dependencies: 我的依赖:

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

Contact class: 联系人类别:

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;

}

The simple interface: 简单的界面:

package com.alco.repository;

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

import com.alco.entity.Contact;

public interface ContactRepository extends JpaRepository<Contact, String> {

}

I think you're missing enabling of the JPA repositories: 我认为您缺少启用JPA存储库的功能:

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

This would be for your configuration class. 这将用于您的配置类。

The only thing I can see is this; 我唯一能看到的就是这个。 please try with changing JpaRepository with Repository 请尝试通过存储库更改JpaRepository

import org.springframework.data.repository.Repository

public interface ContactRepository extends Repository<Contact, String> {

You need to annotate your repository with @Repository , otherwise Spring will not manage your Repository class. 您需要使用@Repository注释存储库,否则Spring将不会管理您的Repository类。

Do not use the more generic @Component , as it may lead to different behavior, eg in Exception Management. 不要使用更通用的@Component ,因为它可能导致不同的行为,例如在异常管理中。 As an example, look at this parte of the Spring Documentation . 作为示例,请查看Spring文档的这一部分

Excerpt: 摘抄:

The postprocessor automatically looks for all exception translators (implementations of the PersistenceExceptionTranslator interface) and advises all beans marked with the @Repository annotation so that the discovered translators can intercept and apply the appropriate translation on the thrown exceptions. 后处理器自动查找所有异常转换器(PersistenceExceptionTranslator接口的实现),并建议所有标有@Repository批注的bean,以便发现的转换器可以拦截适当的转换并将其应用于引发的异常。

Also, as mentioned here , in the future you might have back-compatibility issues. 此外,如提到这里 ,今后你可能有向后兼容性的问题。

As an alternative, you can use @EnableJpaRepositories and specify where Spring should be looking for repositories. 或者,您可以使用@EnableJpaRepositories并指定Spring应该在哪里寻找存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot中的JPA存储库“没有类型的限定bean” - “No qualifying bean of type” for JPA repository in Spring Boot Spring启动测试“没有可用的限定bean” - Spring boot test “No qualifying bean of type available” Spring Boot应用程序中的JUnit测试中没有自动装配JPA存储库的合格Bean - No qualifying bean for autowired JPA repository in JUnit test in Spring Boot application 春季4中没有任何类型的合格Bean - No qualifying bean of type in Spring 4 Spring JPA(Hibernate)没有类型的限定bean:javax.persistence.EntityManagerFactory - Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory Spring Data JPA没有类型的限定bean ...找到依赖项 - Spring Data JPA No qualifying bean of type … found for dependency 没有合格的 bean - FxWeaver 和 Spring Boot - No qualifying bean - FxWeaver and Spring Boot 带有Redis的Spring启动会话中的错误-没有类型为[…SessionRepository]的合格Bean - Error in Spring boot Session with Redis - No qualifying bean of type […SessionRepository] Spring Boot / JUnit-没有可用的&#39;boolean&#39;类型的合格bean - Spring Boot/JUnit - No qualifying bean of type 'boolean' available Spring Boot:在自动装配具体类时没有“找到类型的限定bean” - Spring Boot: “No qualifying bean of type… found” when autowiring concrete class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM