如果我有 @Transactional -对Spring bean中的私有(private)方法进行注解,注解有什么作用吗?
如果 @Transactional
注释在公共(public)方法上,它会起作用并打开一个事务。
public class Bean {
public void doStuff() {
doPrivateStuff();
}
@Transactional
private void doPrivateStuff() {
}
}
...
Bean bean = (Bean)appContext.getBean("bean");
bean.doStuff();
最佳答案
您的问题的答案是否定的 - 如果用于注释私有(private)方法,@Transactional
将无效。代理生成器将忽略它们。
这在 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.
关于java - Spring @Transactional 属性是否适用于私有(private)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4396284/