简体   繁体   中英

Unable to start spring state machine with spring boot

I am trying to implement Spring state machine with different kind of state and event enums.

public enum OrderState {
    ORDER_INITIATED,
    ORDER_CONFIRMED,
    ORDER_CANCELLED,
  }

//Order Events
 public enum OrderEvent {
    ITEM_PICKED,
    ITEM_CONFIRMED,
    ITEM_PACKED,
    ITEM_RETURNED
} 

// Payment States
public enum PaymentState {
    PAYMENT_INITIATED,
    PAYMENT_COLLECTED,
    PAYMENT_CANCELLED,
}

//Payment events
public enum PaymentEvent {
    PAYMENT_START,
    PAYMENT_ID_GENERATED,
    PAYMENT_FAILED
}

//Order state config
@Configuration
@EnableStateMachineFactory(name = "orderStateMachine")
public class OrderStateMachine extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> { 
    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
        states.withStates().initial(OrderState.ITEM_PICKED).end(OrderEvent.ORDER_CONFIRMED)
                .states(EnumSet.allOf(OrderState.class));
    }

    //Rest of code
}


// Payment state config
@Configuration
@EnableStateMachineFactory(name = "paymentStateMachine")
public class PaymentStateMachine extends EnumStateMachineConfigurerAdapter<PaymentState, PaymentEvent> {

     @Override
    public void configure(StateMachineStateConfigurer<PaymentState, PaymentEvent> states) throws Exception {
        states.withStates().initial(PaymentState.PAYMENT_START).end(PaymentState.PAYMENT_ID_GENERATED)
                .states(EnumSet.allOf(PaymentState.class));
    }

    //Rest of code
}


// Service layer
@Service
public class StateMachineService {

    @Autowired
    @Qualifier("orderStateMachine")
    private StateMachineFactory<OrderState, OrderEvent> orderState;

    @Autowired
    @Qualifier("paymentStateMachine")
    private StateMachineFactory<PaymentState, PaymentEvent> paymentState;

    //Rest of code
} 

But When starting the server I am getting below error

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'orderStateMachine' could not be registered. A bean with that name has already been defined in file [/target/classes/com/example/machine/config/OrderStateMachine.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

The error is straightforward, just add the configuration required to have it working well with other spring-data dependencies (root cause of the problem)

Add the configuration bellow to your config files

#YAML
spring:
  main:
    allow-bean-definition-overriding: true


#properties
spring.main.allow-bean-definition-overriding=true

To achieve multiple machines, we added a factory to define which machine service should be served

public class MachineServiceFactory {

@Qualifier("machineA")
  private final StateMachineService<States, Events> machineServiceA;

  @Qualifier("machineB")
  private final StateMachineService< States, Events > machineServiceB;

  public StateMachineService< States, Events > getService(String machineType) {
    return MachineA.equals(machineType) ? machineServiceA : machineServiceB;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM