2016-04-22 44 views
0

我正在使用Play框架开发服务器。在我的几个方法中,我需要执行一些以前的操作(基本上是输入检查),所以我认为这样做的最佳方式是Action Composition在Play框架中组合动作时重复相同动作

我可以用几个注解没有问题

@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) 

那我做错了吗?有没有其他方法可以用不同的值连续多次进行同一动作?

回答

0

最后我做了它的工作

所解释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); 
     } 
    } 

} 

虽然这是一个可行的解决方案是STIL我对我来说似乎有点难看。有没有其他方式我错过了?