Search code examples
hibernatespring-bootspring-transactions

SpringBoot conflicts with Hibernate used in a dependency


I have a Hibernate-based module which works fine when used in a non-SpringBoot application, integrated as a dependency in Maven.

However when trying to add the same module as a dependency to a SpringBoot application, the application fails with:

[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

Is there a way to tell Spring Boot to just back off and let the dependency module handle its own transaction management?


Solution

  • It's been 2+ years since the original post but I recently re-encountered this problem. I spent a few more hours investigating and concluded there is still no legit solution.

    My initial work-around was to defer and postpone the adoption of Spring Boot in the affected project.

    Final work-around is to consolidate all uses of Hibernate through a single microservice with spring-boot-jpa, so that the Hibernate usage is contained within the same project, instead of being pulled in as a transient dependency.