春天在什么@Transactional属性上的私有方法的工作? [英] Does Spring @Transactional attribute work on a private method?
问题描述
如果我有一个<一个href=\"http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/transaction/annotation/Transactional.html\">@Transactional -annotation在一个Spring bean的私有方法,并注释有什么影响?
如果在 @Transactional
注释是一个公共的方法,它的工作原理,并打开一个事务。
公共类的Bean {
公共无效doStuff(){
doPrivateStuff();
}
@Transactional
私人无效doPrivateStuff(){ }
}...豆豆=(豆)appContext.getBean(豆腐);
bean.doStuff();
回答你的问题是没有 - @Transactional
如果用来标注私有方法不会有任何效果。代理生成器将忽略它们。
这是在<一个记录href=\"http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations\">Spring手册章10.5.6 :
方法的可见度和
@Transactional
当使用代理服务器,你应该申请
在@Transactional
批注仅
与公众知名度的方法。如果
你做批注的保护,私有或
与包可见的方法
@Transactional
标注,没有错误
上升,但是注解的方法
不表现出配置的
事务设置。考虑
使用AspectJ的(见下文),如果您需要
注解非公共方法。
块引用>If I have a @Transactional -annotation on a private method in a Spring bean, does the annotation have any effect?
If the
@Transactional
annotation is on a public method, it works and open a transaction.public class Bean { public void doStuff() { doPrivateStuff(); } @Transactional private void doPrivateStuff() { } } ... Bean bean = (Bean)appContext.getBean("bean"); bean.doStuff();
解决方案The answer your question is no -
@Transactional
will have no effect if used to annotate private methods. The proxy generator will ignore them.This is documented in Spring Manual chapter 10.5.6:
Method visibility and
@Transactional
When using proxies, you should apply the
@Transactional
annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the@Transactional
annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
这篇关于春天在什么@Transactional属性上的私有方法的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!