繁体   English   中英

无法使用弹簧启动启动弹簧状态机

[英]Unable to start spring state machine with spring boot

我正在尝试使用不同类型的状态和事件枚举来实现 Spring 状态机。

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
} 

但是当启动服务器时,我遇到了错误

***************************
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

错误很简单,只需添加使其与其他 spring-data 依赖项一起工作所需的配置(问题的根本原因)

将下面的配置添加到您的配置文件中

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


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

为了实现多台机器,我们添加了一个工厂来定义应该服务哪个机器服务

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;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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