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

问题描述

摘自Spring参考文档

From spring reference doc

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

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:

请注意,如果带注释,则此元注释类型无效 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是否在抽象类的具体方法上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:52
查看更多