Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HowTo extend Spring Annotation @Transactional

I have to use 3 different transaction managers in my webapp. So I wrote my own Annotation according to the Spring reference (Section 10.5.6.3 Custom shortcut annotations).

One annotation (for using one specific transactionmanager) looks like this:

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 is working fine when annotating my service layer with the customized @CustomerTX annotation. But I have to provide more options for my annotation, like readonly=true, rollbackFor= and so on. As you cannot "extend" an annotation (I really just need to extend the @Transactional annotation from Spring), whats the correct implementation for this?

like image 363
tim.kaufner Avatar asked Aug 31 '10 09:08

tim.kaufner


1 Answers

In spring 4 you can do that. As stated in the documentation

Meta-annotations can also be combined to create composed annotations. For example, the @RestController annotation from Spring MVC is composed of @Controller and @ResponseBody.

In addition, composed annotations may optionally redeclare attributes from meta-annotations to allow user customization. This can be particularly useful when you want to only expose a subset of the meta-annotation’s attributes. For example, Spring’s @SessionScope annotation hardcodes the scope name to session but still allows customization of the 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;

}
like image 79
C.L.S Avatar answered Oct 05 '22 02:10

C.L.S