13.5.9Using @Transactional with AspectJ

It is also possible to use the Spring Framework’s@Transactionalsupport outside of a Spring container by means of an AspectJ aspect. To do so, you first annotate your classes (and optionally your classes' methods) with the@Transactionalannotation, and then you link (weave) your application with theorg.springframework.transaction.aspectj.AnnotationTransactionAspectdefined in thespring-aspects.jarfile. The aspect must also be configured with a transaction manager. You can of course 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`element and specify themodeattribute toaspectj`as described inSection13.5.6, “Using @Transactional”. Because we’re focusing here on applications running outside of a Spring container, we’ll show you how to do it programmatically.

Prior to continuing, you may want to readSection13.5.6, “Using @Transactional”andChapter7,Aspect Oriented Programming with Springrespectively.
// 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);
When using this aspect, you must annotate theimplementation_class (and/or methods within that class),_not_the interface (if any) that the class implements. AspectJ follows Java’s rule that annotations on interfaces are_not inherited.

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

The@Transactionalannotation on a method within the class overrides the default transaction semantics given by the class annotation (if present). Any method may be annotated, regardless of visibility.

To weave your applications with theAnnotationTransactionAspectyou must either build your application with AspectJ (see theAspectJ Development Guide) or use load-time weaving. SeeSection7.8.4, “Load-time weaving with AspectJ in the Spring Framework”for a discussion of load-time weaving with AspectJ.

results matching ""

    No results matching ""