如何扩展Spring注解@Transactional
我必须在我的网络应用程序中使用 3 个不同的事务管理器。所以我根据 编写了自己的注释Spring参考(第10.5.6.3节自定义快捷方式注释)。
一个注释(用于使用一个特定的事务管理器)如下所示:
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 注释),那么正确的实现是什么?
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{
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Spring 4 中你可以做到这一点。如 中所述文档
元注释也可以组合起来创建组合注释。例如,Spring MVC 中的 @RestController 注解由 @Controller 和 @ResponseBody 组成。
此外,组合注释可以选择重新声明元注释中的属性以允许用户自定义。当您只想公开元注释属性的子集时,这尤其有用。例如,Spring 的 @SessionScope 注释将范围名称硬编码为会话,但仍然允许自定义 proxyMode。
In spring 4 you can do that. As stated in the documentation
Meta-annotations can also be combined to create composed annotations. For example, the @RestController annotation from Spring MVC is composed of @Controller and @ResponseBody.
In addition, composed annotations may optionally redeclare attributes from meta-annotations to allow user customization. This can be particularly useful when you want to only expose a subset of the meta-annotation’s attributes. For example, Spring’s @SessionScope annotation hardcodes the scope name to session but still allows customization of the proxyMode.
恐怕您必须创建多个自定义注释,每个用例一个,并使用您需要的确切
@Transactional
注释对每个注释进行注释。或者您必须在 AspectJ 中编写自己的方面(从 spring-aspects.jar 扩展 org.springframework.transaction.aspectj.AbstractTransactionAspect )来创建您自己的事务逻辑。
更新:这是当时的正确答案,但从 Spring 4 及更高版本开始,CLS 的答案应该是首选
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.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.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