当前位置: 首页 > 面试题库 >

无法在Spring Boot中自动装配@Repository带注释的界面

上官德寿
2023-03-14
问题内容

我正在开发一个Spring Boot应用程序,并且在这里遇到了一个问题。我试图注入一个@Repository注释的接口,它似乎根本不起作用。我收到这个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootRunner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.pharmacy.config.SpringBootRunner.main(SpringBootRunner.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 16 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 18 common frames omitted

这是我的代码:

主要应用类别:

package com.pharmacy.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan("org.pharmacy")
public class SpringBootRunner {


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

Entity class:

package com.pharmacy.persistence.users;

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



@Entity
public class UserEntity {

    @Id
    @GeneratedValue
    private Long id;
    @Column
    private String name;

}

Repository interface:

package com.pharmacy.persistence.users.dao;

import com.pharmacy.persistence.users.UserEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface UserEntityDao extends CrudRepository<UserEntity,Long>{

}

Controller:

package com.pharmacy.controllers;

import com.pharmacy.persistence.users.UserEntity;
import com.pharmacy.persistence.users.dao.UserEntityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HomeController {


    @Autowired
    UserEntityDao userEntityDao;

    @RequestMapping(value = "/")
    public String hello() {
        userEntityDao.save(new UserEntity("ac"));
        return "Test";

    }
}

build.gradle

buildscript {
    ext {
        springBootVersion = '1.2.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
mainClassName = "com.pharmacy.config.SpringBootRunner"
jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}


repositories {
    mavenCentral()
}


dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-ws")
    compile("postgresql:postgresql:9.0-801.jdbc4")

    testCompile("org.springframework.boot:spring-boot-starter-test")
}

application.properties:

spring.view.prefix: /
spring.view.suffix: .html

spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update


spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=abc123

我什至将我的代码与Accessing data jpa进行了比较,但我几乎没有想到此代码有什么问题。任何帮助表示赞赏。提前致谢。

编辑:我按照建议的方式将代码更改为上面的样子,并且在将@Repository接口注入另一个组件时没有出现该错误。但是,我现在有一个问题-无法检索我的组件(我使用了调试)。我做错了什么,所以春天找不到我的零件?


问题答案:

我想分享这类问题的另一个原因,因为我在这个问题上苦苦挣扎了一段时间,因此找不到任何答案。

在类似的存储库中:

@Repository
public interface UserEntityDao extends CrudRepository<UserEntity, Long>{

}

如果实体UserEntity不具有的@Entity对类注释,你会有同样的错误。

对于这种情况,此错误令人困惑,因为您专注于尝试解决有关Spring的问题,但找不到存储库,但问题是实体。



 类似资料:
  • 我正在开发一个spring boot应用程序,我遇到了一个问题。我正在尝试注入一个@Repository注释接口,但它似乎根本不起作用。我收到这个错误 实体类: 存储库接口: 控制器:

  • 问题内容: 我正在看工作区中的一些旧示例。由于没有 @Autowired, 我看不到自动 装配的方式 。Spring Boot + Facebook默认配置。 它工作完美,但是这些bean如何在没有@Autowired的情况下自动进行自动连线? 它们是作为字段或在构造函数中自动接线的吗? 问题答案: 借助Spring Boot 1.4+,构造函数将自动进行自动接线 https://docs.spr

  • 问题内容: 我是Java的新手,所以我希望这不是一个愚蠢的问题。 我在Eclipse中有一个Web项目,正试图将其部署到Tomcat。我在Eclipse中有两个从属项目,它们正在被编译成.jar文件并部署到/ WEB- INF / lib目录中。 很好,但是不幸的是,除非将.jars中的类文件提取到/ WEB-INF / classes目录中,否则Spring不会在依赖项中扫描注释。 有没有一种简

  • 主要内容:1.分析,2.样例讲解1,3.样例讲解2,4.总结1.分析 先看@SpringBootApplication @SpringBootConfiguration:标记当前类为配置类 @EnableAutoConfiguration:开启自动配置 @ComponentScan:扫描主类所在的同级包以及下级包里的Bean @EnableAutoConfiguration: @Import(AutoConfigurationImportSelector.

  • 主要内容:1.SpringBoot自动装配原理,2.BeanFactory和ApplicationContext的区别,3.Spring容器是什么1.SpringBoot自动装配原理 BFPP:BeanFactoryPostProcessor BPP:BeanPostProcessor BDRPP:BeanDefinitionRegistryPostProsessor 1.当启动SpringBoot程序时候,创建SpringApplication的对象,在对象的构造方法中进行对某些参数的初始化工

  • Spring@自动配电 我对Spring@Autowired注释有疑问。请帮助。。。 在Spring mvc中,当我按此顺序尝试@Autow的时候 控制器- 即在控制器I自动连线服务类对象中,在服务类自动连线Dao对象中。 这个注射链工作得很好。 类似地,在strutrs2 Spring中,我以这种方式应用了@Autowired注释 操作--- 该注射链也工作正常。 如果我从这个链之外调用一个fu