简体   繁体   English

Spring 引导中的 UnsatisfiedDependencyException

[英]UnsatisfiedDependencyException in Spring Boot

We are getting No qualifying bean of type for below code in spring boot:我们在 spring 引导中得到以下代码的类型没有合格 bean:

Main class:


package *.*.*;

@SpringBootApplication
public class BankApplication {

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

}
package *.*.*.dataaccess.repository;


@Repository
public interface BankRepository extends JpaRepository<BankEntity, String> {

}

package *.*.*.dataaccess.dao;

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Bank")
public class BankEntity {

//fileds
}



package *.*.*.dataaccess.utils;


@Component
public class DBUtils {

    @Autowired
    BankRepository bnRepo;

    
     */
    public void saveBank(BankDto dto) {
        
        //preparing  bnEntity -entity
        
            this.bnRepo.saveAndFlush(bnEntity);     

    }
}

Added below dependencies related to MySQL: pom.xml:添加以下与 MySQL 相关的依赖项: pom.xml:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.wix/wix-embedded-mysql -->
        <dependency>
            <groupId>com.wix</groupId>
            <artifactId>wix-embedded-mysql</artifactId>
            <version>4.2.0</version>
        </dependency>

getting error while testing this:测试时出错:

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { DBUtils.class })
public class BankTest {

    @Autowired
    DBUtils dbUtils;

    @Test
    public void saveAndFlishTest() {

        dbUtils.saveBank(getBankDetails()); // once this is working then will added junit checks using asserts
    }
}

Error details as below:错误详情如下:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBUtils': Unsatisfied dependency expressed through field 'bnRepo';原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“DBUtils”的bean时出错:通过字段“bnRepo”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ' .嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ' .*.dataaccess.repository.BankRepository' available: expected at least 1 bean which qualifies as autowire candidate. .*.dataaccess.repository.BankRepository' 可用:预计至少有 1 个符合自动装配候选资格的 bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

BankRepository is not created hence the error - there is no bean of that type so there is nothing to inject (via autowiring). BankRepository未创建,因此出现错误 - 没有该类型的 bean,因此没有任何东西可以注入(通过自动装配)。

You should check the following:您应该检查以下内容:

  1. Spring Data is enabled Spring 数据已启用
  2. The BankRepository should reside in the package that is subject to package scanning in your spring boot application BankRepository应驻留在 package 扫描的 package 中

暂无
暂无

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

相关问题 启动时 Spring Boot UnsatisfiedDependencyException - Spring Boot UnsatisfiedDependencyException on startup Spring Boot和Teradata UnsatisfiedDependencyException - Spring Boot and Teradata UnsatisfiedDependencyException 执行时出现 UnsatisfiedDependencyException spring-boot - UnsatisfiedDependencyException occur while executing spring-boot 如何使用mongodb应用程序在spring boot中修复UnsatisfiedDependencyException? - How to fix UnsatisfiedDependencyException in spring boot with mongodb application? Spring的UnsatisfiedDependencyException - UnsatisfiedDependencyException with Spring 如何修复 spring boot 数据 jpa UnsatisfiedDependencyException - How can I fix spring boot data jpa UnsatisfiedDependencyException Spring Boot UnsatisfiedDependencyException 创建名称为不可解析循环引用的 bean 时出错 - Spring Boot UnsatisfiedDependencyException Error creating bean with name unresolvable circular reference 使字段唯一会导致 UnsatisfiedDependencyException - MongoDB、Spring Boot - Making field unique leads to UnsatisfiedDependencyException - MongoDB, Spring Boot Spring Boot:org.springframework.beans.factory.UnsatisfiedDependencyException - Spring Boot:org.springframework.beans.factory.UnsatisfiedDependencyException SpringBoot - UnsatisfiedDependencyException Spring Boot 不是扩展 CrudRepository 的 Autowiring Repository 接口 - SpringBoot - UnsatisfiedDependencyException Spring Boot not Autowiring Repository interface which extends CrudRepository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM