2
votes

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.

2
where is this class DAnCorePrivateRepository ? is it under component scna? Show the package structure of your classpvpkiran
DAnCorePrivateRepository this is interface only i am using SpringBoot only no need for Component scan for that, i have used @SpringBootApplication annotation, that is implicit Component Scan.Sadina Khatun
Make sure DanCorePrivateRepository is located in the same package in @SpringBootApplication application class or it subpackages.Hantsy
Add @Repository on DanCorePrivateRepository interfaceAfridi

2 Answers

4
votes

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.

-2
votes

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.