如何通过依赖项禁用通过 spring.factories 注册的 spring 工厂并保留此 spring-boot 依赖项?

Posted

技术标签:

【中文标题】如何通过依赖项禁用通过 spring.factories 注册的 spring 工厂并保留此 spring-boot 依赖项?【英文标题】:How to disable a spring factory registered via spring.factories by a dependency and keep this spring-boot dependency? 【发布时间】:2021-12-24 06:13:00 【问题描述】:

我正面临一个issue,它有一个由依赖项通过/META-INF/spring.factories 中的条目注册的测试执行侦听器。

我仍然想使用这个依赖项 - 它有很多好东西。

有没有办法以某种方式抑制有问题的(在我的设置中)侦听器的调用?

添加自己的 /META-INF/spring.factories 条目似乎不会覆盖,而是扩展了活动侦听器的列表。

有问题的听众是来自spring-boot-test:2.5.6org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener

【问题讨论】:

【参考方案1】:

特别是对于org.springframework.test.context.TestExecutionListener,可以通过实现DefaultTestExecutionListenersPostProcessor 并将其自身注册为org.springframework.boot.test.context.DefaultTestExecutionListenersPostProcessorMETA-INF/spring.factories 中来过滤掉不需要的听众:

public class SuppressMockitoResetMocksTestExecutionListener implements DefaultTestExecutionListenersPostProcessor 
    @Override
    public Set<Class<? extends TestExecutionListener>> postProcessDefaultTestExecutionListeners(Set<Class<? extends TestExecutionListener>> listeners) 
        return listeners.stream()
                .filter((listener) -> listener != org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.class)
                .collect(Collectors.toSet());
    


https://github.com/mgurov/mockk-v-cached/commit/aea61ccd3afcf13a62736ce33391f0673640d446 上的 Kotlin 示例

通过https://github.com/spring-projects/spring-boot/issues/28604#issuecomment-968657072

【讨论】:

以上是关于如何通过依赖项禁用通过 spring.factories 注册的 spring 工厂并保留此 spring-boot 依赖项?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 AAR 形式集成 leakcanary-android 服务

在 React Native 日历选择器中,我想通过提供一个函数来禁用某些日期

如何在 Spring Boot pom.xml 中临时禁用依赖项 [关闭]

如何禁用 spring-security 登录屏幕?

尽管存在弹簧安全依赖性,如何禁用弹簧安全登录页面? [复制]

通过类名查找 pom 依赖项