java - 如何在 Spring 5 中使用自定义 'TransactionAttributeSource'

标签 java spring transactions spring-transactions

在 Spring 5 中,有ProxyTransactionManagementConfiguration

这定义了:

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

当我在自己的配置中定义自己的 TransactionAttributeSource 时,例如:

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

我收到错误:

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.

如何在 Spring 5 中使用自定义“TransactionAttributeSource”。 (它适用于 Spring 4。)

有什么想法吗? TIA!

最佳答案

解决办法是设置

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

我在这里找到它:Spring Boot Upgrade Error - Invalid bean definition with name org.springframework.transaction.config.internalTransactionalEventListenerFactory

关于java - 如何在 Spring 5 中使用自定义 'TransactionAttributeSource',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038864/

相关文章:

java - 嵌入式 if 语句的替代方案?

java - 附加到某些 javascript 文件的非法字符

java - JPA:映射交叉的 OneToOne 和 ManyToOne 关系

java - 如何在Spring Form Multiple Select中绑定(bind)模型数据

postgresql - PostgreSQL 事务 id (xmin) 是否按顺序出现在提交的版本中?

java - Spring TransactionSynchronizationManager isActualTransactionActive() 和 getCurrentTransactionName() 混淆

java简单的正则表达式

java - 在生产中部署 .jar 文件的正确方法

php - 将 PDO (InnoDB) 事务传递给新类

transactions - 将事务与 JMS (ActiveMQ) 一起使用