存储库注入未被识别为bean

时间:2020-08-07 12:27:33

标签: java spring

大家好

我有这个麻烦的简化版本。我正在尝试运行我的应用程序,但由于无法识别存储库而无法正常注入,因此它失败了。我已经尝试将存储库注释为服务,并添加要扫描的存储库软件包,但是没有任何效果。有人可以帮我吗?

我有例外

说明:

br.com.alura.controller.TopicController中的字段topicRepository需要一个类型为“ br.com.alura.repository.TopicRepository”的bean。

注入点具有以下注释: -@ org.springframework.beans.factory.annotation.Autowired(required = true)

动作:

考虑在您的配置中定义类型为“ br.com.alura.repository.TopicRepository”的bean。

Controller/Service

@Autowired
private TopicRepository topicRepository;

@GetMapping(value = "/api/topics", produces = MediaType.APPLICATION_JSON_VALUE)
public Page<TopicBriefOutputDto> listTopics(TopicSearchInputDto topicSearch, @PageableDefault(sort="creationInstant", direction=Sort.Direction.DESC) Pageable pageRequest) {
    Specification<Topic> topicSearchSpecification = topicSearch.build();
    Page<Topic> topics = this.topicRepository.findAll(topicSearchSpecification, pageRequest);
    return TopicBriefOutputDto.listFromTopics(topics);
}

Start

@SpringBootApplication
@Configuration
@ComponentScan(basePackages ={"br.com.alura.controller"})
@EntityScan
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableSpringDataWebSupport
public class ForumApplication {

public static void main(String[] args) {
    SpringApplication.run(ForumApplication.class, args);
}

}

Repository

public interface TopicRepository extends Repository<Topic, Long>, JpaSpecificationExecutor<Topic> {

}

1 个答案:

答案 0 :(得分:0)

可能您设置的base package不正确。包裹范围太窄。

尝试一下:

@EnableJpaRepositories(basePackages = {"br.com.alura.repository"})

或将br.com.alura.controller更改为br.com.alura

@ComponentScan(basePackages ={"br.com.alura"})