Reputation: 1166
I have extended CrudRepository<ClassName, Id>
in user defined Interface, but while trying to inject using @Autowired
i am getting following given below error :
creating bean with name 'helloController': Unsatisfied dependency expressed through field 'danCorePrivateRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sgcorp.repository.DanCorePrivateRepository' available: expected at least 1 bean which qualifies as autowire candidate.
HelloController.java
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private DanCorePrivateRepository danCorePrivateRepository;
@RequestMapping(value = "/service", method= RequestMethod.GET)
public String selectService(){
String result = "<html>";
result += "<div>"+danCorePrivateRepository.findAll()+"</div>";
return result+ "</html>";
}
}
DanCorePrivateRepository.java (user defined interface)
public interface DanCorePrivateRepository extends CrudRepository<DanaModel, String> {
}
Kindly suggest why its not @Autowired properly?
Note: with some other project it was working.
Upvotes: 2
Views: 6105
Reputation: 264
Please add the @EnableJpaRepositories annotation on top on your configuration class. This @EnableJpaRepositories annotation has the basePackages or basePackageClasses attribute, through which you can specify the packages (which are annotated with @Repository) to be scanned by Spring Data JPA.
Upvotes: 5
Reputation: 517
I think you have missed annotations @RepositoryRestResource
and @Repository
on your user defined Interface DanCorePrivateRepository
. You have to mark it as follows -
@Repository
@RepositoryRestResource
public interface DanCorePrivateRepository extends CrudRepository<DanaModel, String> {
}
@RepositoryRestResource
annotation will direct Spring to create RESTful endpoints for your repository.
Upvotes: -2