Play Framework中的合成操作时重复相同的操作

时间:2016-04-22 09:49:31

标签: java playframework annotations playframework-2.5

我正在使用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 exampleVerboseAnnotation所以我不认为在这里写它是值得的。由于我的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)

那么我做错了什么?有没有其他方法可以使用不同的值多次链接同一个动作?

1 个答案:

答案 0 :(得分:0)

最后我让它发挥作用

As Explained in here Java 8编译器不需要编写RepeatableAction2但是它在编译时添加它,所以我需要为该注释添加一个实现:

@With(RepeatableAction2Impl.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatableAction2 {
    Action2[] value() default {};
}

在实现中我手动链接所有操作:

public class RepeatableAction2Impl extends Action<RepeatableAction2> {

    @Override
    public CompletionStage<Result> call(Http.Context ctx) {
        if (configuration.value().length > 0) {
            int actions = configuration.value().length;
            List<Action<Action2>> actionList = new ArrayList<>();
            // Create actions
            for (int i = 0; i < actions; i++) {
                Action2Impl action2Impl = new Action2Impl();
                action2Impl.configuration = configuration.value()[i];
                actionList.add(action2Impl);
            }
            // Chaining
            actionList.get(actions - 1).delegate = delegate;
            for (int i = 0; i < actions - 1; i++) {
                actionList.get(i).delegate = actionList.get(i + 1);
            }
            // Delegate the work to actions
            return actionList.get(0).call(ctx);
        } else {
            return delegate.call(ctx);
        }
    }

}

虽然这是一个有效的解决方案,但对我来说仍然有点难看。还有其他方法我错过了吗?