24 Ekim 2021 Pazar

SpringBoot StateMachine Guard Arayüzü

Giriş
Şu satırı dahil ederiz
import org.springframework.statemachine.Guard;
Kullanım
Örnek
Şöyle yaparız
@Configuration
@EnableStateMachine
public class OrderStateMachineConfig extends 
  EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {

  @Override
  public void configure(
    StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions)
  throws Exception {
    transitions
      .withExternal()
        .source(OrderState.PICKUP).event(OrderEvent.INITIATE_REFUND)
        .target(OrderState.REFUND_INITIATED)
        .guard(new RefundInitiateGuard())
      .and()
      .withExternal()
      ...
  }
}
evaluate metodu
Örnek
Şöyle yaparız
public class RefundInitiateGuard implements Guard<OrderState,OrderEvent> {

  @Override
  public boolean evaluate(StateContext<OrderState, OrderEvent> stateContext) {
    ...
    return true;
  }
}

Hiç yorum yok:

Yorum Gönder