如何访问 spring StateMachineConfig 数据

How to access spring StateMachineConfig data

我有一个 spring 状态机,我配置了一个 EnumStateMachineConfigurerAdapter 注释 @EnableStateMachineFactory

一切顺利,我可以使用工厂创建状态机:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MyStateMachineConfig.class);
context.refresh();
StateMachineFactory stateMachineFactory = context.getBean(StateMachineFactory.class);

但我需要对状态机的配置做一些反思。基本上我想获得所有最终状态的列表,以供我记录。

此信息在 org.springframework.statemachine.config.StateMachineConfig 中可用,我可以在调试时知道它的一个实例已创建。但是好像没有被注册成bean,我也不能访问它作为调用:

context.getBean(StateMachineConfig.class);

原因:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.statemachine.config.StateMachineConfig<?, ?>' available

您可以注册自己的 StateMachineModelVerifier and access the States data - there's a public isEnd() method on the StateData 对象。

@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config)
        throws Exception {
    config
        .withVerifier()
            .enabled(true)
            .verifier(cusomVerifier());
}

@Bean    
public StateMachineModelVerifier customVerifier() {
  return new CustomVerifier();
}

class CustomVerifier extends DefaultStateMachineModelVerifier {
  @Override
  public void verify(StateMachineModel model) {
    super.verify(model); // call the existing sanity check model verifier
    Collection<StateData<State, Event> statesData = model.getStatesData().getStateData(); 
    //log your stuff
  }
}