gpt4 book ai didi

java - 如何忽略 spring.factories 中定义的 ApplicationListener?

转载 作者:行者123 更新时间:2023-12-04 16:30:40 26 4
gpt4 key购买 nike

我正在我的项目中导入一个 Spring Boot Starter,因为它包含一个我想使用但我不想运行自动配置的类。我可以在启动器中看到有一个 META-INF/spring.factories 文件,它同时定义了自动配置和应用程序监听器。

# Auto Configurations
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.demo.SomeAutoConfiguration,\
org.demo.AnotherAutoConfiguration

# Application Listeners
org.springframework.context.ApplicationListener=\
org.demo.SomeApplicationListener,\
org.demo.AnotherApplicationListener

我已经想出了如何从自动配置中排除特定类,这很好用。

@SpringBootApplication(exclude={SomeAutoConfiguration.class, AnotherAutoConfiguration.class})

现在我似乎无法弄清楚如何排除这些应用程序监听器中的一个或多个。有什么想法吗?

最佳答案

没有内置支持忽略某些应用程序监听器。但是,您可以继承 SpringApplication , 覆盖 SpringApplication.setListeners(Collection<? extends ApplicationListener<?>>)并过滤掉你不想要的听众:

    new SpringApplication(ExampleApplication.class) {

@Override
public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
super.setListeners(listeners
.stream()
.filter((listener) -> !(listener instanceof UnwantedListener))
.collect(Collectors.toList()));
}

}.run(args);

关于java - 如何忽略 spring.factories 中定义的 ApplicationListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45445548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com