首页 > 解决方案 > Spring中如何解决双bean冲突

问题描述

我正在尝试同时使用两个库,GraphQL 和 Jmix。

我使用 Intellij 的新项目向导(安装了 Jmix 插件)创建了 Jmix 项目,然后使用标准的 graphql-spring-boot-starter 将 GraphQL 添加到 Gradle。然后我写了一个模式和解析器bean。

但是在启动过程中,由于 WebSocket 端点 /subscriptions 在 Tomcat 上注册了两次,因此引发了异常。(我尝试使用应用程序属性 graphql.servlet.subscriptions.websocket.path 更改端点,但这不是问题。)

经过一番挖掘,我发现来自 graphql-spring-boot-autoconfigure的类GraphQLWebsocketAutoConfiguration和来自 jmix-ui-starter 的VaadinAutoConfiguration都注册了一个 ServerEndpointExporter bean,这是不应该发生的。

这是graphql的代码:

@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(ServerContainer.class)
public ServerEndpointExporter serverEndpointExporter() {
   return new ServerEndpointExporter();
}

这里是 Jmix 的:

@Bean
public ServerEndpointExporter websocketEndpointDeployer() {
    return new VaadinWebsocketEndpointExporter();
}

GraphQL 被标记为 ConditionalOnMissingBean,但在另一个之前注册,因此不会触发条件。

如何禁用这两个 bean 之一,或设置它们的优先级?

我设法通过完全禁用 GraphQL 的 websocket 服务来解决这个问题:

graphql.servlet.websocket.enabled = false

但我想知道如何解决这类问题。

标签: springspring-bootdependency-injection

解决方案


不幸的是,看起来配置是错误的,所以没有什么你可以用类似 Spring 的方式“惯用地”做的。

我能想到的所有解决方案都是变通方法,解决这个问题的最佳方法可能是在相应的团队中打开一个缺陷,以便他们可以引入一个允许以两种方式禁用配置的属性。好吧,也许他们已经这样做了,谁知道呢。

至于可能的解决方案:

  1. 使用属性:graphql.servlet.websocket.enabled=false 它将禁用 graphql 配置。而且您将能够自己重新定义您需要的豆子。例如,如果需要,只定义:ServerEndpointRegistration和bean。ServerEndpointExporter警告 - 我没有检查 graphql 库的源文件,也许这个属性在其他地方使用。

  2. 尝试重新定义您想要/不想加载的 bean 的 BeanDefinition。这可以通过 BeanFactoryPostProcessor 完成:

@Component
public class SampleBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
     // find the bean definition of the bean you need and try to:
     // - set "primary" on it
     String bdName = ... here put the name of the bean you want to fine-tune
     BeanDefinition beanDefinition =  beanFactory.getBeanDefinition(bdName);
     beanDefinition.setPrimary(true);
     // ... or ... 
     // - remove it altogether from the application context (bean factory)
     ((BeanDefinitionRegistry)beanFactory).removeBeanDefinition(bdName);

只是为了澄清-当bean定义已经解析但在实际注入发生之前,spring在应用程序启动期间调用bean工厂后处理器。


推荐阅读