Spring Boot:加载所有在测试中实现接口的bean?

     2023-02-27     75

关键词:

【中文标题】Spring Boot:加载所有在测试中实现接口的bean?【英文标题】:Spring Boot: load all beans implementing an interface in test? 【发布时间】:2020-12-19 20:22:51 【问题描述】:

我有一些接口Filter 和这个接口的一些实现。

@Test 中,我想加载所有实现Filter 的bean。

目前,我必须明确声明它们。例如:

@ContextConfiguration(classes = FilterA.class, FilterB.class)

问题:引入另一个Filter 需要为每个使用filters 的测试类添加它。

有没有更好的办法?

【问题讨论】:

【参考方案1】:

添加TestConfiguration 并在配置中声明所有实现该接口的bean。然后在您的测试中添加@Import(TestConfiguration.class)

这样,如果有额外的实现,您只需将其添加到TestConfiguration

您唯一需要注意的是使用@Qualifier 注释添加适当的接口实现。

【讨论】:

通过编写带有@Bean 注释的方法并返回new 对象? 是的。确保使用 @TestConfiguration 注释你的类。这会将其标记为测试配置类而不是常规配置类,因此不会干扰您在应用程序中的 bean 设置。

在spring boot中实现自己的内存缓存机制

】在springboot中实现自己的内存缓存机制【英文标题】:Implementowninmemorycachemechanisminspringboot【发布时间】:2021-12-2323:11:19【问题描述】:我被要求在我的SpringBoot应用程序中实现内存缓存机制。我的申请与社交媒体有关。用户可以... 查看详情

如何在 Spring Boot 中实现基于角色权限的系统

】如何在SpringBoot中实现基于角色权限的系统【英文标题】:HowtoimplementRolePermissionbasedsysteminSpringBoot【发布时间】:2016-11-0919:14:30【问题描述】:我正在尝试使用springboot和springsecurity来实现基于角色权限的系统。为此,我以http://w... 查看详情

在 Spring Boot 中实现“注销”功能

】在SpringBoot中实现“注销”功能【英文标题】:Implement\'logout\'functionalityinSpringBoot【发布时间】:2014-07-0221:53:37【问题描述】:为了让基本的安全功能正常工作,我在我的pom.xml中添加了以下启动包<dependency><groupId>org.spri... 查看详情

如何在 Spring Boot 中实现刷新令牌

】如何在SpringBoot中实现刷新令牌【英文标题】:HowtoimplementrefreshtokeninSpringBoot【发布时间】:2018-09-2709:08:06【问题描述】:我已按照本指南https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/在我的Web应用程序中实现访问令牌... 查看详情

如何使用hibernate在spring boot中实现分页

】如何使用hibernate在springboot中实现分页【英文标题】:Howtoimplementpaginationinspringbootwithhibernate【发布时间】:2015-12-0217:41:44【问题描述】:我正在使用带有休眠功能的SpringBoot,并且我想在我的项目中使用分页。我在谷歌上搜索并... 查看详情

在spring boot中实现自定义错误

】在springboot中实现自定义错误【英文标题】:Implementthecustomerrorinspringboot【发布时间】:2019-04-0702:58:26【问题描述】:我想页面。在应用程序中,id是主键,所以当没有给出id时,它会将请求传输到页面,但我希望应用程序在索... 查看详情

如何在 Spring Boot 中实现 Camunda SendTask

】如何在SpringBoot中实现CamundaSendTask【英文标题】:HowtoimplementCamundaSendTaskinSpringBoot【发布时间】:2020-10-0914:06:21【问题描述】:我是Camunda的新手,正在使用以下任务:业务规则任务-->(网关)-->发送任务-->用户任务在使... 查看详情

无法在 Spring Boot 中实现 Drools KieSession Persistence

】无法在SpringBoot中实现DroolsKieSessionPersistence【英文标题】:CannotimplementDroolsKieSessionPersistenceinSpringBoot【发布时间】:2021-01-2102:17:00【问题描述】:我试图在SpringBootMaven项目中使用KieSession的持久性功能实现Drools。关注thisdocumentatio... 查看详情

如何在spring boot中实现jms队列

】如何在springboot中实现jms队列【英文标题】:Howtoimplementjmsqueueinspringboot【发布时间】:2017-04-1708:58:00【问题描述】:我已经使用初始化程序创建了一个SpringBoot项目,我正在尝试创建我的第一条消息,但我不知道从哪里开始。我... 查看详情

如何在 Spring Boot 中实现部分 GET 请求?

】如何在SpringBoot中实现部分GET请求?【英文标题】:HowtoimplementpartialGETrequestsinSpringBoot?【发布时间】:2021-09-1918:47:58【问题描述】:我正在尝试实现一个控制器,该控制器将接受请求标头中的字节范围,然后将多媒体作为字节... 查看详情

在现有 Spring Boot REST 应用程序中实现时区的最佳策略 [重复]

】在现有SpringBootREST应用程序中实现时区的最佳策略[重复]【英文标题】:BestStrategytoimplementTimeZoneinanexistingSpringBootRESTapplication[duplicate]【发布时间】:2017-08-0611:06:33【问题描述】:为现有应用程序实施TimeZone的任何最佳策略。因... 查看详情

如何注入一个继承自另一个在 Spring 中实现的接口的接口

】如何注入一个继承自另一个在Spring中实现的接口的接口【英文标题】:HowtoinjectaninterfacethatinheritsfromanotherinterfacethathasanimplementationinSpring【发布时间】:2022-01-1517:19:40【问题描述】:我的应用程序的基础设施包中有一个接口和... 查看详情

获取在 .NET Core 中实现接口的所有类型

】获取在.NETCore中实现接口的所有类型【英文标题】:Gettingalltypesthatimplementaninterfacein.NETCore【发布时间】:2016-11-2110:51:18【问题描述】:使用反射,我如何获得所有在.NETCore中实现特定接口的类型?我注意到.NET4.6中可用的方法不... 查看详情

运行 Spring Boot 集成测试时出现 Liquibase 问题

】运行SpringBoot集成测试时出现Liquibase问题【英文标题】:Liquibaseissuewhilerunningspringbootintegrationtest【发布时间】:2019-05-3021:40:45【问题描述】:我正在尝试在创建liqubasebean并运行所有变更集的SpringBoot中运行集成测试。在yaml文件中... 查看详情

如何在优雅地spring中实现消息的发送和消费

本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQClient端集成为spring-boot-starter框架的开发细节,然后通过一个简单的示例来一步一步的讲解如何使用这个spring-boot-starter工具包来配置,发送和... 查看详情

是否可以在spring boot/java中实现每个用户在不同时间动态调度一个任务

】是否可以在springboot/java中实现每个用户在不同时间动态调度一个任务【英文标题】:isitpossibletoachievedynamicschedulingofataskforeachuseratdifferenttimeinspringboot/java【发布时间】:2021-08-0301:08:23【问题描述】:我们有一个RESTAPI“/schedule”... 查看详情

在自己的授权服务器中实现 OAUTH 2.0 的 Spring Boot 2

】在自己的授权服务器中实现OAUTH2.0的SpringBoot2【英文标题】:Springboot2withOAUTH2.0implementationinownauthorizationserver【发布时间】:2018-12-0723:28:40【问题描述】:我创建了一个新的springboot2项目,以创建带有eureka服务器的微服务,使用zu... 查看详情

如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 Spring 服务中

】如何在SpringBoot中实现通用JPA存储库-它可以自动装配到任何实体/类类型的Spring服务中【英文标题】:HowtoimplementGenericJPARepositoryinSpringBoot-Whichcanbeautowiredintospringservicesforanyentity/classtype【发布时间】:2016-02-0408:07:27【问题描述】... 查看详情