如何扩展 Spring Annotation @Transactional [英] HowTo extend Spring Annotation @Transactional

查看:37
本文介绍了如何扩展 Spring Annotation @Transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的 web 应用程序中使用 3 个不同的事务管理器.所以我根据写了自己的AnnotationSpring 参考(第 10.5.6.3 节自定义快捷方式注释).

I have to use 3 different transaction managers in my webapp. So I wrote my own Annotation according to the Spring reference (Section 10.5.6.3 Custom shortcut annotations).

一个注释(用于使用一个特定的事务管理器)如下所示:

One annotation (for using one specific transactionmanager) looks like this:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.transaction.annotation.Transactional;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("customer")
public @interface CustomerTX{


}

使用自定义的 @CustomerTX 注释注释我的服务层时,一切正常.但是我必须为我的注释提供更多选项,例如 readonly=true、rollbackFor= 等.由于您无法扩展"注释(我真的只需要从 Spring 扩展 @Transactional 注释),那么正确的实现是什么?

Everything is working fine when annotating my service layer with the customized @CustomerTX annotation. But I have to provide more options for my annotation, like readonly=true, rollbackFor= and so on. As you cannot "extend" an annotation (I really just need to extend the @Transactional annotation from Spring), whats the correct implementation for this?

推荐答案

恐怕您将不得不创建多个自定义注释,每个用例一个,每个注释都使用准确的 @Transactional 您需要的注释.

You will have to create several custom annotations, I'm afraid, one for every use case, annotating each with the exact @Transactional annotation you need.

或者您必须在 AspectJ 中编写自己的方面(从 spring-aspects.jar 扩展 org.springframework.transaction.aspectj.AbstractTransactionAspect )来创建自己的事务逻辑.

Or you will have to write your own aspect in AspectJ ( extend org.springframework.transaction.aspectj.AbstractTransactionAspect from spring-aspects.jar ) to create your own transaction logic.

更新:这是当时的正确答案,但从 Spring 4 开始,CLS 的答案应该是首选

Update: this was the correct answer at the time, but as of Spring 4 and later, the answer by C.L.S should be preferred

这篇关于如何扩展 Spring Annotation @Transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆