【问题标题】:Spring state machine annotation @WithStateMachine not working when created in wrong orderSpring状态机注释@WithStateMachine在以错误顺序创建时不起作用
【发布时间】:2016-10-24 14:39:00
【问题描述】:

我正在使用 spring 状态机,并希望使用 @WithStateMachine、@OnTransition 和 @OnStateChanged 为转换和状态更改添加功能,以使使用 @EnableStateMachine 的类尽可能简单。虽然我从来没有让它工作,但经过一些试验和错误后,我意识到必须在状态机之前创建 @WithStateMachine bean,但这种依赖关系不会自动解决。

我的项目中有趣的部分基本上可以归结为以下几点:

package org.myorg.a

@Component
public class MyComponent {

    @Autowired
    StateMachine<States, Events> sm;

}

package org.myorg.b

@WithStateMachine
public class Listener {

    @OnTransition
    public void anyTransition() {}
}

MyComponent,因此状态机将在 Listener 之前创建,因为 Spring 将在包 b 之前查看包 a,实际上,Listener 类中的任何方法都不会与状态机关联,并且永远不会调用 anyTransition 方法.

我能想到的唯一两个解决方案是

  • 重命名包
  • 使用@DependsOn 注释@EnableStateMachine 类并显式依赖所有@WithStateMachine 类

在我看来,这两种解决方案都不好。有没有人有更好的方法来解决这个问题?

【问题讨论】:

    标签: java spring spring-statemachine


    【解决方案1】:

    希望这个问题刚刚在 https://github.com/spring-projects/spring-statemachine/issues/232 中得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 2018-03-18
      • 2012-06-24
      • 1970-01-01
      • 2015-10-26
      • 2016-06-02
      • 1970-01-01
      相关资源
      最近更新 更多