HowTo Extension Spring Abstract @Transactional

I need to use 3 different transaction managers in my webapp. Therefore, I wrote my own annotation according to the Spring link (section 10.5.6.3 Custom shortcut annotations).

One annotation (to use one specific transaction operator) is as follows:

import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.transaction.annotation.Transactional; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("customer") public @interface CustomerTX{ } 

Everything works great when annotating my level of service using the @CustomerTX annotation. But I have to provide more options for my annotation, such as readonly = true, rollbackFor = and so on. Since you cannot "extend" the annotation (I just need to extend the @Transactional annotation from Spring), why do I need the correct implementation?

+6
java spring annotations transactional
source share
2 answers

You will need to create several custom annotations, I'm afraid, one for each use case, annotating each of them with the exact @Transactional annotation.

Or you will need to write your own aspect in AspectJ (extend org.springframework.transaction.aspectj.AbstractTransactionAspect from spring -aspects.jar) to create your own transaction logic.

+3
source share

In spring 4 you can do this. As indicated in the documentation

Meta annotations can also be combined to create compiled annotations. For example, the @RestController annotation from spring MVC consists of @Controller and @ResponseBody.

In addition, compiled annotations may optionally override attributes from meta annotations to allow customization. This can be especially useful if you want to show only a subset of the meta annotation attributes. For example, the Spring s @SessionScope annotation hardcodes the name of the session area, but still allows you to configure proxyMode.

 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Scope(WebApplicationContext.SCOPE_SESSION) public @interface SessionScope { /** * Alias for {@link Scope#proxyMode}. * <p>Defaults to {@link ScopedProxyMode#TARGET_CLASS}. */ @AliasFor(annotation = Scope.class) ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS; } 
0
source share

All Articles