Spring自定义注释:如何继承属性?

Lai*_*jus 7 java annotations spring-annotations

我正在创建自己的自定义快捷方式注释,如Spring Documentation中所述:

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

是否有可能,通过我的自定义注释,我还可以设置其他可用的属性@Transactional?我想能够使用我的注释,例如,像这样:

@CustomTransactional(propagation = Propagation.REQUIRED)
public class MyClass {

}
Run Code Online (Sandbox Code Playgroud)

Bij*_*men 4

不,如果您想要额外的属性,则必须以这种方式在自定义注释本身上设置其他属性,这是行不通的:

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

一种解决方案(不好的:-))可能是使用您在场景中看到的一组基本案例来定义多个注释:

@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 {
}
Run Code Online (Sandbox Code Playgroud)