当前位置: 首页 > 知识库问答 >
问题:

Spring Boot中的UnsatisfiedDependencyException

楚嘉胜
2023-03-14
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);     

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

原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“DBUtils”的bean时出错:通过字段“bn repo”表示的未满足的依赖项;嵌套异常为org.springframework.beans.factory.noSuchBeanDefinitionException:没有“.*.DataAccess.Repository.BankRepository”类型的合格bean可用:应至少有一个合格的自动候选bean。依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}

共有1个答案

谭志用
2023-03-14

bankrepository没有创建,因此会出现错误-没有这种类型的bean,因此没有任何东西可以注入(通过自动连接)。

您应该检查以下内容:

  1. 启用Spring数据
  2. BankRepository应该驻留在spring引导应用程序中要进行包扫描的包中
 类似资料:
  • 本文向大家介绍SpringBoot中的Thymeleaf用法,包括了SpringBoot中的Thymeleaf用法的使用技巧和注意事项,需要的朋友参考一下 Thymeleaf Thymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。 我们为什么要用Thymeleaf来作为模板引擎呢?官网给了我们一个非常令人信服的解释: Thymeleaf is a m

  • 本文向大家介绍SpringBoot中的Thymeleaf模板,包括了SpringBoot中的Thymeleaf模板的使用技巧和注意事项,需要的朋友参考一下 一、前言     Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1、JSP 最明显的问题在于它看起来像HTML或XML,但它其实上并不是。大多数的JS

  • 我有一个控制器 服务接口 我想在我的控制器中使用@autowired来使用该服务,但当我运行应用程序时,我得到以下错误 org.springframework.beans.factory.beanCreationException:创建名为“demo application”的bean时出错:注入autowired依赖项失败;嵌套异常为org.SpringFramework.Beans.Facto

  • null 非常感谢你的帮助

  • 本文向大家介绍Springboot中@Value的使用详解,包括了Springboot中@Value的使用详解的使用技巧和注意事项,需要的朋友参考一下 Springboot通过@Value注解将配置文件中的属性注入到容器内组件中(可用在@Controller、@Service、@Configuration、@Component等Spring托管的类中)  1.普通字符串注入 例:yml中存在key

  • Eureka Discovery服务器(包括/config/*服务器上的云配置服务)正在向Eureka服务器注册,并且Eureka仪表板显示了该实例。此服务在端口9001上运行,启用SSL并进行用户身份验证。服务照常工作。 然后我创建了一个新的SpringBoot服务,它连接到Eureka并注册到它。由于使用了自签名证书,我在这篇文章中编写了一个小的SSLConfguration类:如何覆盖Spr

  • 我们有一个SpringBoot(版本1.5.12)REST Api,带有springfox-swagger2和springfox-swagger-ui(版本2.9.2) 我可以在http://localhost:8080/swagger-ui.html看到Swagger UI 我如何配置swagger-ui来读取我的swagger.yaml/json配置文件,而不是自动生成它?我尝试了几个配置都没

  • 我正在尝试对我的Spring Boot应用程序进行dockerize。 这是我的docker作曲文件 我的dockerfile 我可以看到我的战争已经展开。在日志中我可以看到 在/usr/local/tomcat/webapps目录中,我可以看到这个根目录。war和根文件夹存在。我用以下命令启动docker: 当我访问localhost:8080Tomcat说Http状态404-未找到。 这里是执