简体   繁体   English

无法在 spring boot 应用程序中自动装配接口

[英]Cannot Autowire interface in spring boot app

I am developing a REST API using spring boot.我正在使用 spring boot 开发 REST API。 Following is my package structure Getting the following exception when I try to start my application以下是我的包结构 当我尝试启动我的应用程序时出现以下异常

***************************
APPLICATION FAILED TO START
***************************

Description:

Field articleRepository in com.abc.service.ArticleService required a bean of type 'com.abc.dao.ArticleRepository' that could not be found.


Action:

Consider defining a bean of type 'com.abc.dao.ArticleRepository' in your configuration.

Following is my project structure-以下是我的项目结构-

com.abc
com.abc.bean
    -Article.java
com.abc.controller
    -ArticleController.java
com.abc.dao
    -ArticleRepository.java (Interface)
com.abc.service
    -ArticleService.java
com.abc.web
    -AbcApplication.java (main Springboot class)

In AbcApplication.java as it is not in the root package, I have the below annotations在 AbcApplication.java 因为它不在根包中,所以我有以下注释

@SpringBootApplication
@ComponentScan(basePackages="com.abc.*")

I tried few ways -我尝试了几种方法 -

  • I moved AbcApplication.java to root package com.abc but no success我将 AbcApplication.java 移动到根包 com.abc 但没有成功
  • Instead of interface(ArticleRepository.java) I made it a class, it is working而不是接口(ArticleRepository.java)我把它变成了一个类,它正在工作
  • I keep it as interface but changed annotation from @Repository to @Service/Component still no success.我将它保留为接口,但将注释从 @Repository 更改为 @Service/Component 仍然没有成功。

I am confused how it is working if I change it to class instead of interface.如果我将它更改为类而不是接口,我很困惑它是如何工作的。

@Repository
public interface ArticleRepository {

}

You don't have any bean for ArticleRepository.您没有任何用于 ArticleRepository 的 bean。 If you will use Spring Data Jpa you have to extend a type of Repository: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories .如果您将使用 Spring Data Jpa,则必须扩展一种存储库: https ://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories。

If you will use your own repository, you must implement it.如果您将使用自己的存储库,则必须实现它。

If you are using any RDBMS and if ArticleRepository repository is responsible for interacting with your database, then you need to either extends CrudRepository or JpaRepository in your ArticleRepository , then only Spring will be able to create the bean of ArticleRepository ,and you will be able to autowire your repository.如果您使用任何 RDBMS 并且ArticleRepository存储库负责与您的数据库交互,那么您需要在ArticleRepository中扩展CrudRepositoryJpaRepository ,那么只有 Spring 才能创建ArticleRepository的 bean,并且您将能够自动装配您的存储库。

If you are not extending any of CrudRepository or JpaRepository ,then at the time of bean creation, ArticleRepository is only plain java interface and a plain interface can not be instantiated.如果您没有扩展任何CrudRepositoryJpaRepository ,那么在创建 bean 时, ArticleRepository只是普通的 java 接口,普通接口不能被实例化。

And as for your question:至于你的问题:

Instead of interface(ArticleRepository.java) I made it a class, it is working

Its because when you declare it as a class, then Spring does instantiate a concrete class, so actual object will be created at the bean creation time and everything will be working as it should be.这是因为当您将其声明为一个类时,Spring 会实例化一个具体类,因此将在创建 bean 时创建实际对象,并且一切都将按应有的方式运行。

Ideally you should have one class that implements interface ArticleRepository.理想情况下,您应该有一个实现接口 ArticleRepository 的类。 Annotate that class with @Repository, spring will take care of wiring.用@Repository 注释该类,spring 将负责接线。

I was also encountered with the same issue.我也遇到了同样的问题。 What I missed was the spring-boot-starter-jpa dependency.我错过的是spring-boot-starter-jpa依赖项。 It worked when I added this dependency in the pom.xml file.当我在 pom.xml 文件中添加此依赖项时,它起作用了。

MyBatis gives you two samples to access your databases with spring boot.@ Component or @Mapper. MyBatis 为您提供了两个使用 spring boot 访问数据库的示例。@Component 或@Mapper。

Sample link: mybatis-spring-boot-sample-xml示例链接: mybatis-spring-boot-sample-xml

Suggestion: Show your ArticleRepository code fully.建议:完整显示您的ArticleRepository代码。

interface does not require any annotation (neither @repository not @Service).接口不需要任何注解(@repository 和@Service 都不需要)。

If its a DAO file add @Repository annotation to the class implementing the corresponding interface.如果它是一个 DAO 文件,请在实现相应接口的类中添加 @Repository 注解。

And, if its a service then add @Service annotation to the class implementing the corresponding interface.并且,如果它是一项服务,则将@Service 注释添加到实现相应接口的类中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM