Reputation: 1274
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 am confused how it is working if I change it to class instead of interface.
@Repository
public interface ArticleRepository {
}
Upvotes: 3
Views: 6087
Reputation: 11
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.
Upvotes: 1
Reputation: 839
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.
Upvotes: 0
Reputation: 601
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.
Upvotes: 2
Reputation: 1
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.
Upvotes: 0
Reputation: 1346
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.
Upvotes: 3
Reputation: 8347
Ideally you should have one class that implements interface ArticleRepository . Annotate that class with @Repository, spring will take care of wiring.
Upvotes: 1