Spring @transaction是否在抽象类的具体方法上工作 [英] Does Spring @transaction work on a concrete method of abstract class

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

问题描述

摘自Spring参考文档

From spring reference doc

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上使用了@Transactional在抽象超类上,因此您实际上已经正确配置了Spring事务管理.本身用@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进行了注释,请查看它是

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

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

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