Spring @transaction 是否适用于抽象类的具体方法 [英] Does Spring @transaction work on a concrete method of abstract class

查看:77
本文介绍了Spring @transaction 是否适用于抽象类的具体方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自弹簧参考文档

Spring 建议您只使用 @Transactional 注释来注释具体类(和具体类的方法),而不是注释接口.您当然可以将 @Transactional 注释放在接口(或接口方法)上,但是如果您使用基于接口的代理,这只能像您期望的那样工作.Java 的事实注释不是从接口继承的意味着如果您使用基于类的代理 (proxy-target-class="true") 或基于编织的方面 (mode="aspectj"),那么事务设置不会被代理和编织基础设施,并且对象不会被包装在事务代理中,这绝对是糟糕的.

Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.

虽然只讨论了接口,但抽象类也被认为是非具体的.

Though it only talks about interfaces, abstract classes are considered as non-concrete as well.

所以如果我有一个抽象类

So if i have an abstract class

public abstract class BaseService{
//here is a concrete method
@Transactional
public void updateData{
//do stuff using dao layer
}

和一个扩展类的具体类

public class SpecialService extends BaseService{
//body of class
}

现在如果我从控制器类调用 specialService.updateData() 会是事务性的吗?

Now if i call specialService.updateData() from my controller class will it be transactional?

推荐答案

在抽象超类上使用 @Transactional will 假设您实际上已经正确配置了 Spring 事务管理工作,因为 @Transactional 本身用 @Inherited 注释,并且来自 Javadoc 我们有:

Granting that you have actually configured Spring transaction management correctly, using @Transactional on an abstract superclass will work, since @Transactional is itself annotated with @Inherited and from it's Javadoc we have:

表示自动继承注解类型.如果注释类型上存在继承的元注释声明,用户查询类的注解类型声明,并且类声明对此没有注释类型,然后会自动查询该类的超类注释类型.这个过程会一直重复,直到一个注解找到此类型,或者类层次结构(对象)的顶部是到达.如果没有超类具有此类型的注释,则查询将表明相关类没有这样的注释.

Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.

注意这个元注解类型在注解的时候没有效果type 用于注释除类以外的任何内容.还要注意的是这个元注释只会导致注释被继承超类;已实现接口上的注解无效.

Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.

要实际看到 @Transactional 是用 @Inherited 注释的,请查看它的 Javadoc

To actually see that @Transactional is annotated with @Inherited check out it's Javadoc

这篇关于Spring @transaction 是否适用于抽象类的具体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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