Using @Transactional with AspectJ

您还可以在 Spring 容器外部使用 Spring Framework 的 @Transactional 支持,方法是使用 AspectJ 切面。要做到这一点,首先使用 @Transactional 注解对您的类(以及您的类的方法)进行注释,然后使用 org.springframework.transaction.aspectj.AnnotationTransactionAspect 中定义的 spring-aspects.jar 将您的应用程序链接(编织)起来。您还必须用事务管理器配置切面。您可以使用 Spring Framework 的 IoC 容器来处理切面的依赖注入。配置事务管理切面的最简单方法是使用 <tx:annotation-driven/> 元素,并按 Using @Transactional 中所述将 mode 属性指定给 aspectj。因为我们此处专注于在 Spring 容器外部运行的应用程序,所以我们将向您展示如何以编程方式进行操作。

You can also use the Spring Framework’s @Transactional support outside of a Spring container by means of an AspectJ aspect. To do so, first annotate your classes (and optionally your classes' methods) with the @Transactional annotation, and then link (weave) your application with the org.springframework.transaction.aspectj.AnnotationTransactionAspect defined in the spring-aspects.jar file. You must also configure the aspect with a transaction manager. You can use the Spring Framework’s IoC container to take care of dependency-injecting the aspect. The simplest way to configure the transaction management aspect is to use the <tx:annotation-driven/> element and specify the mode attribute to aspectj as described in Using @Transactional. Because we focus here on applications that run outside of a Spring container, we show you how to do it programmatically.

在继续之前,您可能想要分别阅读 Using @TransactionalAOP

Prior to continuing, you may want to read Using @Transactional and AOP respectively.

以下示例展示了如何创建事务管理器并配置 AnnotationTransactionAspect 以使用它:

The following example shows how to create a transaction manager and configure the AnnotationTransactionAspect to use it:

  • Java

  • Kotlin

// construct an appropriate transaction manager
DataSourceTransactionManager txManager = new DataSourceTransactionManager(getDataSource());

// configure the AnnotationTransactionAspect to use it; this must be done before executing any transactional methods
AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);
// construct an appropriate transaction manager
val txManager = DataSourceTransactionManager(getDataSource())

// configure the AnnotationTransactionAspect to use it; this must be done before executing any transactional methods
AnnotationTransactionAspect.aspectOf().transactionManager = txManager

当您使用此方面时,您必须对实现类(或该类中的方法或两者)进行注释,而不是该类实现的接口(如果有)。AspectJ 遵循 Java 的规则,即接口上的注释不会被继承。

When you use this aspect, you must annotate the implementation class (or the methods within that class or both), not the interface (if any) that the class implements. AspectJ follows Java’s rule that annotations on interfaces are not inherited.

类上的 @Transactional 注解指定了类中任何公共方法执行的默认事务语义。

The @Transactional annotation on a class specifies the default transaction semantics for the execution of any public method in the class.

类中方法上的 @Transactional 注解会覆盖由类注解给出的默认事务语义(如果存在)。你可以注释任何方法,无论可见性如何。

The @Transactional annotation on a method within the class overrides the default transaction semantics given by the class annotation (if present). You can annotate any method, regardless of visibility.

要使用`AnnotationTransactionAspect`编织你的应用程序,你必须使用AspectJ构建你的应用程序(请参阅https://www.eclipse.org/aspectj/doc/released/devguide/index.html[AspectJ开发指南])或使用加载时编织。请参阅Load-time weaving with AspectJ in the Spring Framework了解有关使用AspectJ进行加载时编织的讨论。

To weave your applications with the AnnotationTransactionAspect, you must either build your application with AspectJ (see the AspectJ Development Guide) or use load-time weaving. See Load-time weaving with AspectJ in the Spring Framework for a discussion of load-time weaving with AspectJ.