【问题标题】:SpringBoot conflicts with Hibernate used in a dependencySpringBoot 与依赖项中使用的 Hibernate 冲突
【发布时间】:2019-04-06 03:29:10
【问题描述】:

我有一个基于 Hibernate 的模块,在非 SpringBoot 应用程序中使用时工作正常,集成为 Maven 中的依赖项。

但是,当尝试将相同的模块作为依赖项添加到 SpringBoot 应用程序时,应用程序失败并显示:

[2019-04-05 20:27:14.157] - 2060 WARNING [main] --- org.springframework.context.annotation.AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'org.springframework.transaction.config.internalTransactionAdvisor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration; factoryMethodName=transactionAdvisor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]] for bean 'org.springframework.transaction.config.internalTransactionAdvisor': There is already [Root bean: class [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
[2019-04-05 20:27:14.173] - 2060 SEVERE [main] --- org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter: 

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

Description:

The bean 'org.springframework.transaction.config.internalTransactionAdvisor', defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class], 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

有没有办法让 Spring Boot 退出并让依赖模块处理自己的事务管理?

【问题讨论】:

  • 在不知道您的依赖项和代码的情况下很难找到问题,但我猜您已经通过 Spring 和您自己的模块获得了两个 Hibernate 依赖项和相关 bean。难道你不能简单地在你的 Spring 应用程序中去掉 Hibernate 或者重写你的模块配置以只生成与 Hibernate 相关的 bean(如果还没有的话)?

标签: hibernate spring-boot spring-transactions


【解决方案1】:

我认为您的问题是自动配置,添加此代码进行自动配置:

@AutoConfigureBefore 和@AutoConfigureAfter

【讨论】:

    【解决方案2】:

    距离最初的帖子已经 2 年多了,但我最近再次遇到了这个问题。我又花了几个小时调查并得出结论,仍然没有合法的解决方案。

    我最初的解决方法是推迟和推迟在受影响的项目中采用 Spring Boot。

    最后的解决方法是通过一个带有spring-boot-jpa 的微服务来整合 Hibernate 的所有使用,以便将 Hibernate 使用包含在同一个项目中,而不是作为临时依赖项引入。

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 2016-11-05
      • 2019-08-08
      • 2021-11-05
      • 2018-11-20
      • 1970-01-01
      • 2018-10-24
      • 2016-09-17
      相关资源
      最近更新 更多