Can't Autowire @Repository annotated interface in Spring Boot Can't Autowire @Repository annotated interface in Spring Boot spring spring

Can't Autowire @Repository annotated interface in Spring Boot


When the repository package is different to @SpringBootApplication/@EnableAutoConfiguration, base package of @EnableJpaRepositories is required to be defined explicitly.

Try to add @EnableJpaRepositories("com.pharmacy.persistence.users.dao") to SpringBootRunner


I had the same issues with Repository not being found. So what I did was to move everything into 1 package. And this worked meaning that there was nothing wrong with my code. I moved the Repos & Entities into another package and added the following to SpringApplication class.

@EnableJpaRepositories("com...jpa")@EntityScan("com...jpa")

After that, I moved the Service (interface & implementation) to another package and added the following to SpringApplication class.

@ComponentScan("com...service")

This solved my issues.


There is another cause for this type of problem what I would like to share, because I struggle in this problem for some time and I could't find any answer on SO.

In a repository like:

@Repositorypublic interface UserEntityDao extends CrudRepository<UserEntity, Long>{}

If your entity UserEntity does not have the @Entity annotation on the class, you will have the same error.

This error is confusing for this case, because you focus on trying to resolve the problem about Spring not found the Repository but the problem is the entity. And if you came to this answer trying to test your Repository, this answer may help you.