在 Spring Boot 项目中,我在从组件扫描中排除某些存储库时遇到问题。
我有一个包含一些实体和一些存储库(JpaRepositories)的库。由于某种原因,我实现了一个小型 Spring Boot Data Rest 应用程序,用于让测试人员快速访问实体。因此,我实现了一个扩展 PagingAndSortingRepository 的存储库,并用 @RepositoryRestResource 进行注释。
当应用程序启动时,将扫描所有存储库并使其可用。只要我只想让 Data Rest 存储库可用,我就对组件扫描器进行注释以排除不需要的存储库。但这行不通。我检查了执行器 bean 端点以及我所做的任何事情 - 不排除任何存储库。
为了演示这个问题,我创建了一个简单的演示应用程序:https://github.com/magomi/springboot-restdata-repoloading https://github.com/magomi/springboot-restdata-repoloading.
为了排除 DataRepository 我尝试了两种方法:
// exclude V02
@SpringBootApplication
@ComponentScan(excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
DataRepository.class})
})
and
// exclude V01
@SpringBootApplication(exclude = { DataRepository.class })
没有成功。当我调用 /beans 端点(由 Spring Boot 执行器提供)时,我总是看到
{
bean: "dataRepository",
aliases: [ ],
scope: "singleton",
type: "org.codefromhell.test.repoloading.DataRepository",
...
},
{
bean: "dataApiRepository",
aliases: [ ],
scope: "singleton",
type: "org.codefromhell.test.repoloading.api.DataApiRepository",
...
},