SpringBoot 与依赖中使用的 Hibernate 冲突

SpringBoot conflicts with Hibernate used in a dependency

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

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

[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 引导退出并让依赖模块处理它自己的事务管理?

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

@AutoConfigureBefore 和@AutoConfigureAfter

自最初 post 以来已经有 2 年多了,但我最近 re-encountered 这个问题。我又花了几个小时调查并得出结论,仍然没有合法的解决方案。

我最初的 work-around 是推迟并 postpone 在受影响的项目中采用 Spring 引导。

最终work-around是通过spring-boot-jpa的单个微服务来整合Hibernate的所有使用,这样Hibernate的使用就包含在同一个项目中,而不是作为一个瞬时依赖被拉进来.