javaspringtransactionsspring-transactions

How to use a custom 'TransactionAttributeSource' with Spring 5


In Spring 5 there is ProxyTransactionManagementConfiguration.

This defines:

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionAttributeSource transactionAttributeSource() {
    return new AnnotationTransactionAttributeSource();
}

When I define my own TransactionAttributeSource in my own configuration like:

@Bean
@Primary
public TransactionAttributeSource transactionAttributeSource() {
    return new RollbackOnAllTransactionAttributeSource();
}

I got the error:

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'transactionAttributeSource' defined in class path resource [...]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=[...]; factoryMethodName=transactionAttributeSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [...]] for bean 'transactionAttributeSource': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration; factoryMethodName=transactionAttributeSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]] bound.

How can I use a custom 'TransactionAttributeSource' with Spring 5. (It works with Spring 4.)

Any ideas? TIA!


Solution

  • The solution is to set

    spring.main.allow-bean-definition-overriding=true
    

    I found it here: Spring Boot Upgrade Error - Invalid bean definition with name org.springframework.transaction.config.internalTransactionalEventListenerFactory