Reputation: 15929
I am new to Spring Boot and want to autowire a repository from a different package in a Rest Controller. It seems that when I place the interface and implementation in a different package the actual controller the autowire seems to fail.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.acme.repository.RawDataRepository] found for dependency:..
Controller:
package com.acme.controller;
import com.acme.repository.RawDataRepository;
// imports removed!
@RestController
@EnableAutoConfiguration
@ComponentScan("com.acme")
public class DataCollectionController {
@Autowired
private RawDataRepository repository;
// code removed!
}
I have tried to use the @ComponentScan annotation but this gives no solution. Any idea what i am missing? Whenever i put the interface into the package in which the controller resides then all goes well.
Upvotes: 5
Views: 9823
Reputation: 1284
You have to use following two annotations
@EnableJpaRepositories(basePackages = "package-name")
@EntityScan(basePackages = "package-name")
EnableJpaRepositories will enable repository if main class is in some different package. You need to use EntityScan as well to point to package where you have your entity beans or else it will fail with 'Bean is not of managed type' error.
Upvotes: 4
Reputation: 323
Spring Boot Provides annotations for enabling Repositories. So whenever someone uses any Repository (it can be JPARepository , CassandraReposotory) it should be enabled in Application Class itself.
Example:
@EnableCassandraRepositories("package name")
@EnableJpaRepositories("package name")
After providing the above annotations, the container takes care of injecting beans for repositories as well.
Upvotes: 1
Reputation: 58094
If you have Spring Data @Repositories
in a different package you have to explicitly @EnableJpaRepositories
(or replace "Jpa" with your own flavour). Boot takes it's defaults from the package containing the @EnableAutoConfiguration
so it might work to just move that class as well.
Upvotes: 9