简体   繁体   中英

Cannot Autowire interface in spring boot app

I am developing a REST API using spring boot. 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

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

I tried few ways -

  • I moved AbcApplication.java to root package com.abc but no success
  • Instead of interface(ArticleRepository.java) I made it a class, it is working
  • I keep it as interface but changed annotation from @Repository to @Service/Component still no success.

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. 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 .

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.

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.

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.

Ideally you should have one class that implements interface ArticleRepository. Annotate that class with @Repository, spring will take care of wiring.

I was also encountered with the same issue. What I missed was the spring-boot-starter-jpa dependency. It worked when I added this dependency in the pom.xml file.

MyBatis gives you two samples to access your databases with spring boot.@ Component or @Mapper.

Sample link: mybatis-spring-boot-sample-xml

Suggestion: Show your ArticleRepository code fully.

interface does not require any annotation (neither @repository not @Service).

If its a DAO file add @Repository annotation to the class implementing the corresponding interface.

And, if its a service then add @Service annotation to the class implementing the corresponding interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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