Spring:在 Java 配置中定义自定义 @Transactional 行为 [英] Spring: define custom @Transactional behavior in Java config
问题描述
我希望 Spring 在使用 @Transactional
注释的方法上回滚事务,以防该方法抛出已检查的异常.相当于:
I would like Spring to rollback a transaction on methods annotated with @Transactional
in case the method throws a checked exception. An equivalent of this:
@Transactional(rollbackFor=MyCheckedException.class)
public void method() throws MyCheckedException {
}
但我需要将此行为作为所有 @Transactional
注释的默认值,而无需在任何地方编写它.我们使用Java来配置Spring(配置类).
But I need this behavior to be default for all @Transactional
annotations without the need to write it everywhere. We are using Java to configure Spring (configuration classes).
我尝试了 spring 文档,仅在 XML 中可用.所以我尝试创建这个 XML 文件:
I tried the configuration suggested by spring documentation, which is only available in XML. So I tried to create this XML file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" rollback-for="com.example.MyCheckedException" />
</tx:attributes>
</tx:advice>
</beans>
... 并通过 @ImportResource
导入.Spring 确实识别并解析了该文件(一开始我有一些错误),但它不起作用.@Transactional
的行为没有改变.
... and import it via @ImportResource
. Spring did recognize and parse the file (I had some errors in it at first), but it doesn't work. The behavior of @Transactional
has not changed.
我也尝试定义我自己的事务属性源,如这个答案中所建议的.但它也使用了 XML 配置,所以我不得不像这样将它转换成 Java:
I also tried defining my own transaction property source, as suggested in this answer. But it also used the XML configuration so I had to transform it into Java like this:
@Bean
public AnnotationTransactionAttributeSource getTransactionAttributeSource() {
return new RollbackForAllAnnotationTransactionAttributeSource();
}
@Bean
public TransactionInterceptor getTransactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
transactionInterceptor.setTransactionAttributeSource(transactionAttributeSource);
return transactionInterceptor;
}
@Bean
public BeanFactoryTransactionAttributeSourceAdvisor getBeanFactoryTransactionAttributeSourceAdvisor(TransactionAttributeSource transactionAttributeSource) {
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
advisor.setTransactionAttributeSource(transactionAttributeSource);
return advisor;
}
这也不起作用 - Spring 一直使用自己的事务属性源(与在配置中创建的实例不同).
This also didn't work - Spring kept using its own transaction property source (different instance than the one which was created in the configuration).
在 Java 中实现这一目标的正确方法是什么?
What is the correct way to achieve this in Java?
推荐答案
你应该实现自己的注解 - 参考
You should rather implement own annotation - reference
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(rollbackFor=MyCheckedException.class)
public @interface TransactionalWithRollback {
}
这篇关于Spring:在 Java 配置中定义自定义 @Transactional 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!