如何使用 Spring Boot 从不同的包自动装配存储库接口? [英] How to Autowire repository interface from a different package using Spring Boot?

查看:27
本文介绍了如何使用 Spring Boot 从不同的包自动装配存储库接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Boot 的新手,想从 Rest Controller 中的不同包自动装配存储库.似乎当我将接口和实现放在不同的包中时,自动装配的实际控制器似乎失败了.

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: 找不到类型为 [com.acme.repository.RawDataRepository] ​​的合格 bean 依赖项:..

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.acme.repository.RawDataRepository] found for dependency:..

控制器:

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!
}

我尝试使用@ComponentScan 注释,但这没有给出解决方案.知道我缺少什么吗?每当我将接口放入控制器所在的包中时,一切都会顺利.

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.

推荐答案

如果您在不同的包中有 Spring Data @Repositories,您必须显式 @EnableJpaRepositories(或用您自己的口味替换Jpa").Boot 从包含 @EnableAutoConfiguration 的包中获取它的默认值,因此它也可能只移动该类.

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.

这篇关于如何使用 Spring Boot 从不同的包自动装配存储库接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆