首页 > 解决方案 > 使用 JHipster-Registry(Cloud Config Server 作为中央服务器)集中微服务时 Bean-Definition Overriding Exception

问题描述

我目前在 JHipster-registry 中集中微服务客户端配置时遇到问题。我正在使用文件系统方法来集中注册表应用程序的配置 (central-config) 文件夹。

在运行时启动微服务客户端应用程序期间,我收到此异常,如下所示


        ██╗ ██╗   ██╗ ████████╗ ███████╗   ██████╗ ████████╗ ████████╗ ███████╗
        ██║ ██║   ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗
        ██║ ████████║    ██║    ███████╔╝ ╚█████╗     ██║    ██████╗   ███████╔╝
  ██╗   ██║ ██╔═══██║    ██║    ██╔════╝   ╚═══██╗    ██║    ██╔═══╝   ██╔══██║
  ╚██████╔╝ ██║   ██║ ████████╗ ██║       ██████╔╝    ██║    ████████╗ ██║  ╚██╗
   ╚═════╝  ╚═╝   ╚═╝ ╚═══════╝ ╚═╝       ╚═════╝     ╚═╝    ╚═══════╝ ╚═╝   ╚═╝

:: JHipster ?  :: Running Spring Boot 2.1.6.RELEASE ::
:: https://www.jhipster.tech ::

2020-09-14 12:09:12.061  INFO 3828 --- [  restartedMain] c.t.g.GlueResourceDataServiceApp         : The following profiles are active: dev
2020-09-14 12:09:16.314  WARN 3828 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'processorStorageGateway' defined in null: Cannot register bean definition [Generic bean: class [org.springframework.integration.gateway.GatewayProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'processorStorageGateway': There is already [Generic bean: class [org.springframework.integration.gateway.GatewayProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
2020-09-14 12:09:16.342 ERROR 3828 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'processorStorageGateway', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

我尝试过使用注册表应用程序的中央配置文件夹中的属性、.yml 文件(其中包含客户端应用程序的中央配置)的方法

spring:
    
    main:
        allow-bean-definition-overriding: true

但还没有运气。我也尝试在其 bean 注入失败的接口上定义 @Service 注释,根据上面的错误,如下所示

@MessagingGateway
@Service
public interface ProcessorStorageGateway { 
..... }

我看到了两个参考类文件,它们使用这个接口来自动装配 FailJobscheduler 和 ExternalQueuelistener,它们试图在运行时注入这个接口类型的 bean 'processorGateway',重命名它们的自动装配属性也无助于解决问题,如下所述

@Component
public class FailJobsScheduler {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JobService jobService;

    @Autowired
    private ProcessorStorageGateway processorStorageGateway;
............
}

@Service
public class ExternalQueueListener {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProcessorStorageGateway processorStorageGateway;
.....
}

由于 Spring Boot-2.1 及更高版本在运行时不允许默认 bean 覆盖,目前卡在这个问题上。上面开头提到的命中和试炼也无济于事。任何人都可以就面临的这个问题和解决它的最佳方法提供建议吗?

标签: spring-bootjhipsterjhipster-registryjhipster-gateway

解决方案


上述问题已通过避免一些重复的 Spring 注释来解决,这些注释已经存在于项目中,在进一步调查期间(如 @Configuration 等)以及在主 Spring-BOOT 应用程序类中,通过提及组件扫描来执行相应的包帮助解决了这个问题


推荐阅读