简体   繁体   English

Spring Data JPA-未找到符合条件的Bean

[英]Spring data JPA - No qualifying bean found for dependency

I am going to have a simple test with spring data jpa . 我将使用spring data jpa进行简单测试。

I have a simple pojo , an interface , and a runner application. 我有一个简单的pojo ,一个接口和一个runner应用程序。

Here is my code: 这是我的代码:

package aa.bb.cc.repository;

@Repository
public interface ContentRepository extends CrudRepository<Content, Long>{
}

And, i have a simple POJO : 而且,我有一个简单的POJO

@Entity
@Table(name = "content")
public class Content {

public Content(String name, String title, String description) {
    this.name = name;
    this.title = title;
    this.description = description;
}

@NotNull
private String name;

@NotNull
private String title;

@NotNull
private String description;
...
}

And, Application class: 并且, Application类:

package aa.bb.cc.repository;

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner demo(ContentRepository repository) {
        return (args) -> {
            // save two contents
            repository.save(new Content("name1", "title1", "description1"));

            // fetch all Contents
            log.info("Contents found with findAll():");

            for (Content eachContent : repository.findAll()) {
                log.info(eachContent.toString());
            }
            log.info("");
        };
    }
}

My pom.xml : 我的pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.185</version>
</dependency>

I get this exception: 我得到这个例外:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [aa.bb.cc.repository.ContentRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

I saw some related problems but can't solve this problem. 我看到了一些相关问题,但无法解决此问题。 What is the solution? 解决办法是什么?

UPDATE 更新

spring-config.xml : spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <jpa:repositories base-package="aa.bb.cc.repository"/>

    <context:annotation-config/>

</beans>

Try to put ContentRepository , Content and Application in the same package. 尝试将ContentRepositoryContentApplication放在同一包中。 If tou need different packages, annotate Application with: 如果您需要其他软件包,请使用以下注释为Application程序添加注释:

@EnableJpaRepositories("repository.package")
@EntityScan("entities.package")
@ComponentScan("other.components.package")

According to specializt answer you can add the @Repository annotation: 根据specializt答案,您可以添加@Repository批注:

package aa.bb.cc.repository;

@Repository
public interface ContentRepository extends CrudRepository<Content, Long>{
}

Well your example works in my env without xml configuration. 好了,您的示例可以在没有xml配置的环境中使用。 The two things that I had to fix was: 我必须修复的两件事是:

  • Add no-parameter constructor 添加无参数构造函数
  • Add identifier field with @Id annotation ( @Id private Long id = 5l; ) 添加带有@Id注释的标识符字段( @Id private Long id = 5l;

If you still have a problem with this, I can upload it to my github repository and post link here. 如果您仍然对此有疑问,可以将其上传到我的github存储库中,并在此处发布链接。

我建议阅读官方文档 ,您需要在spring配置中激活存储库软件包,如下所示:

<repositories base-package="aa.bb.cc.repository" />

暂无
暂无

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

相关问题 Spring Data JPA没有类型的限定bean ...找到依赖项 - Spring Data JPA No qualifying bean of type … found for dependency 无法使用 Spring Data JPA 正确连接 @Service,找不到依赖的合格 bean - Unable to get @Service wired correctly with Spring Data JPA, No qualifying bean found for dependency 找不到依赖类型的合格Bean - No qualifying bean of type found for dependency 在Spring Boot单表中找不到依赖项类型的合格Bean - No qualifying bean of type found for dependency in Spring Boot single table Spring @Autowire 失败,没有找到类型的合格 bean 依赖错误 - Spring @Autowire fails with No qualifying bean of type found for dependency error Spring Neo4j:NoSuchBeanDefinitionException:找不到依赖项类型为[org.springframework.data.neo4j.support.Neo4jTemplate]的合格bean - Spring Neo4j: NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.neo4j.support.Neo4jTemplate] found for dependency Spring JPA:XML 配置 - 没有符合条件的存储库 bean/找不到元素“jpa:repositories”的声明 - Spring JPA: XML Configuration - No qualifying bean of Repository / no declaration can be found for element 'jpa:repositories' Spring-Boot创建名称不完整的bean将导致“ NoSuchBeanDefinitionException,找不到依赖类型为[]的合格bean” - Spring-Boot create bean with out name will cause “NoSuchBeanDefinitionException, No qualifying bean of type[]found for dependency ” NoSuchBeanDefinitionException:未找到依赖关系类型为JpaVendorAdapter的合格Bean - NoSuchBeanDefinitionException: No qualifying bean of type JpaVendorAdapter found for dependency 找不到符合@Autowired服务的类型的限定Bean - No Qualifying Bean of type found for dependency for @Autowired service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM