我正在使用 Play Framework 开发服务器。在我的几种方法中,我需要执行一些先前的操作(基本上是输入检查),所以我认为最好的方法是Action Composition。
我可以毫无问题地使用多个注释
@Action1 // <---------------------------------------- This action is executed
@Action2(value = "someValue") // <------------------- This action is executed
public CompletionStage<Result> doSomething() {
...
}
但是,一旦我尝试重复其中一项具体操作不会执行的操作:
@Action1 // <---------------------------------------- This action is executed
@Action2(value = "someValue") // <------------------- This action is not executed
@Action2(value = "someOtherValue") // <-------------- This action is not executed
public CompletionStage<Result> doSomething() {
...
}
我的Action1
注释看起来像Play Framework 示例VerboseAnnotation
的注释,所以我认为不值得在这里写。由于我的注释可以重复,我声明了这样的注释:Action2
RepeatableAction2
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatableAction2 {
Action2[] value() default {};
}
Action2
看起来像这样:
@With(Action2Impl.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(value = RepeatableAction2.class)
public @interface Action2 {
String value();
}
该方法已正确注释。当我添加:
for (Method m : Application.class.getDeclaredMethods()) {
RequiredJsonValues reqs = m.getAnnotation(RequiredJsonValues.class);
for (RequiredJsonValue req : reqs.value()) {
System.out.println("Method: " + m + " annotation: " + req);
}
}
在我得到的方法的开头
Method: public java.util.concurrent.CompletionStage controllers.SomeController.doSomething() annotation: @util.Action2(value=someValue)
Method: public java.util.concurrent.CompletionStage controllers.SomeController.doSomething() annotation: @util.Action2(value=someOtherValue)
那么我做错了什么?有没有其他方法可以用不同的值多次链接相同的操作?