Spring user annotation: how to inherit attributes?

I create my own shortcut annotation as described in the Spring Documentation :

@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional(value = "Custom", readOnly = true) public @interface CustomTransactional { } 

Is it possible that with my custom annotation, I could also set any other attributes available in @Transactional ? I would like to use my annotation, for example, as follows:

 @CustomTransactional(propagation = Propagation.REQUIRED) public class MyClass { } 
+6
source share
2 answers

No, this will not work if you need additional attributes that should be set in your user annotation as follows:

 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional(value = "Custom", readOnly = true, propagation = Propagation.REQUIRED) public @interface CustomTransactional { } 

The solution (bad one :-)) may be to define a few annotations with the basic set of cases that you see for your scenario:

 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional(value = "Custom", readOnly = true, propagation = Propagation.REQUIRED) public @interface CustomTransactionalWithRequired { } @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional(value = "Custom", readOnly = true, propagation = Propagation.SUPPORTED) public @interface CustomTransactionalWithSupported { } 
+4
source

In (at least) Spring 4, you can do this by specifying an element inside an annotation, for example:

 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional(value = "Custom", readOnly = true) public @interface CustomTransactional { Propagation propagation() default Propagation.SUPPORTED; } 

Source: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-meta-annotations

+2
source

All Articles