java - Spring @Transactional 属性是否适用于私有(private)方法?

标签 java spring transactions annotations

如果我有 @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/

相关文章:

ruby-on-rails-4 - 创建交易时过滤掉不必要的fields_for记录

java - 使用 JPA 的事务边界和 DTO 转换

java - java中以字符开头并后跟数字的子字符串

java数据绑定(bind)到gui,就像在c#中一样

java - @Value 在 SpEL 中如何工作?

java - 如何更改 Spring Boot 错误响应中的状态代码?

java - 如何在oracle中查看由启用hibernate的java程序执行的最近sql命令的事务日志(带有时间和性能信息)

java - 如何证明我的 Double.toString 算法对于所有值都是正确的?

JavaFX ComboBox 不显示 COM 端口

java - 如何将主面自动完成对象放入支持 bean 中