我已延长 CrudRepository<ClassName, Id>
在用户定义的接口(interface)中,但在尝试使用 @Autowired
注入(inject)时我收到以下错误:
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(用户定义接口(interface))
public interface DanCorePrivateRepository extends CrudRepository<DanaModel, String> {
}
请提出为什么它不正确@Autowired?
注意:在其他一些项目中它正在运行。
最佳答案
请在您的配置类顶部添加 @EnableJpaRepositories 注释。这个@EnableJpaRepositories注解具有basePackages或basePackageClasses属性,通过它们可以指定Spring Data JPA扫描的包(带有@Repository注解的包)。
关于java - Spring : Unsatisfied dependency expressed through field, CrudRepository扩展接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299696/