使用 Spring Data Rest 时如何从组件扫描中排除 @Repository [英] How to exclude a @Repository from component scan when using Spring Data Rest

查看:53
本文介绍了使用 Spring Data Rest 时如何从组件扫描中排除 @Repository的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring Boot 项目中,我在从组件扫描中排除某些存储库时遇到问题.

in a spring boot project I have problems to exclude some repositories from the component scan.

我有一个包含一些实体和一些存储库 (JpaRepositories) 的库.出于某种原因,我实现了一个小型 Spring Boot Data Rest 应用程序,用于让测试人员快速访问实体.因此,我实现了一个扩展 PagingAndSortingRepository 并使用 @RepositoryRestResource 注释的存储库.

I have a library that contains some entities and some repositories (JpaRepositories). For some reason I implemented a small Spring Boot Data Rest application that shall be used to give testers a quick access to the entities. Therefore I implemented a repository that extends the PagingAndSortingRepository and is annotated with @RepositoryRestResource.

当应用程序启动时,将扫描所有存储库并使其可用.只要我只想让 Data Rest 存储库可用,我就会对 componenten 扫描器进行注释以排除不需要的存储库.但这不起作用.我检查了执行器 bean 端点以及我所做的一切 - 不排除任何存储库.

When the application starts all repository will be scanned and made available. As long as I only want to have the Data Rest repositories available I annotated the componenten scanner to exclude the unwant repositories. But this doesn't work. I checked with the actuator beans endpoint and whatever I do - no repositories are excluded.

为了演示这个问题,我创建了一个简单的演示应用程序:https://github.com/magomi/springboot-restdata-repoloading.

To demonstrate the problem I created a simple demo application: https://github.com/magomi/springboot-restdata-repoloading.

为了排除 DataRepository,我尝试了两种方法:

To exclude the DataRepository I tried the two approaches:

// exclude V02
@SpringBootApplication
@ComponentScan(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
                DataRepository.class})
})

// exclude V01
@SpringBootApplication(exclude = { DataRepository.class })

没有成功.当我调用/beans 端点(由 spring boot 执行器提供)时,我总是看到

Without success. When I call the /beans endpoint (provided by spring boot actuator) I always see

{
    bean: "dataRepository",
    aliases: [ ],
    scope: "singleton",
    type: "org.codefromhell.test.repoloading.DataRepository",
    ...
},
{
    bean: "dataApiRepository",
    aliases: [ ],
    scope: "singleton",
    type: "org.codefromhell.test.repoloading.api.DataApiRepository",
    ...
},

推荐答案

因为它是一个仓库而不是严格意义上的 @Component,你需要通过添加 @EnableJpaRepositories 来排除它> 到您的应用程序:

Because it's a repository and not strictly a @Component, you need to excluded it by adding @EnableJpaRepositories to your application:

@SpringBootApplication
@EnableJpaRepositories(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
                DataRepository.class})
})
public class ApiApplication {

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

这篇关于使用 Spring Data Rest 时如何从组件扫描中排除 @Repository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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