简体   繁体   English

Spring:在Java配置中定义自定义@Transactional行为

[英]Spring: define custom @Transactional behavior in Java config

I would like Spring to rollback a transaction on methods annotated with @Transactional in case the method throws a checked exception. 我希望Spring在使用@Transactional注释的方法上回滚事务,以防该方法引发已检查的异常。 An equivalent of this: 等效项:

@Transactional(rollbackFor=MyCheckedException.class)
public void method() throws MyCheckedException {

}

But I need this behavior to be default for all @Transactional annotations without the need to write it everywhere. 但是我需要所有@Transactional注释都将此行为设置为默认行为,而无需到处编写它。 We are using Java to configure Spring (configuration classes). 我们正在使用Java来配置Spring(配置类)。

I tried the configuration suggested by spring documentation , which is only available in XML. 我尝试了spring文档建议的配置,该配置仅在XML中可用。 So I tried to create this XML file: 因此,我尝试创建此XML文件:

<?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>

... and import it via @ImportResource . ...并通过@ImportResource导入。 Spring did recognize and parse the file (I had some errors in it at first), but it doesn't work. Spring确实识别并解析了该文件(起初我在其中存在一些错误),但是它不起作用。 The behavior of @Transactional has not changed. @Transactional的行为未更改。

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: 但是它也使用了XML配置,因此我不得不像这样将其转换为Java:

@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;
}

This also didn't work - Spring kept using its own transaction property source (different instance than the one which was created in the configuration). 这也不起作用-Spring继续使用其自己的事务属性源(与配置中创建的实例不同的实例)。

What is the correct way to achieve this in Java? 用Java实现此目的的正确方法是什么?

You should rather implement own annotation - reference 您应该实施自己的注释- 参考

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(rollbackFor=MyCheckedException.class)
public @interface TransactionalWithRollback {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM