java - Spring : Unsatisfied dependency expressed through field, CrudRepository扩展接口(interface)

标签 java spring spring-data-jpa crud autowired

我已延长 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/

相关文章:

java - 通过集成测试执行生产代码时@Autowired 字段为 null

java - 在 JPA 存储库中使用查询是否是正确的做法?

java - 当我们说一个特定的数据结构是缓存友好的时,这意味着什么?

java - 如何执行网络抓取以在 Google App Engine 上查找 Java 中的特定链接页面?

java - 在java中将unix时间戳转换为日期

java - Spring validator 抛出异常

java - 从Java应用程序发送电子邮件中的异常:中继被拒绝

java - AnnotationException 引用的属性不是 (One|Many)ToOne

java - 使用 Maven 进行目标特定的 Java 更新

java - spring jpa中根据实体从多个表中获取数据