是非之声,无翼而飞;损益之名,无胫而走。——白居易

文档:

https://docs.spring.io/spring-statemachine/docs/current/reference/#configuring-guards

说白了守卫是用来判断事件执行后能否更新到下一个状态的

这里按之前提到的代码示例来示范

首先是配置为返回true,发现下面一路正常打印

然后是return false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.ruben.parent;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {

@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
config
.withConfiguration()
.autoStartup(true)
.listener(listener());
}

@Override
public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
states
.withStates()
.initial(States.READY)
.state(States.GAME, gameStateMachine())
.and()
.withStates()
.parent(States.GAME)
.initial(States.NIGHT)
.state(States.DAY);
}

@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
transitions
.withExternal()
.source(States.READY).target(States.GAME).event(Events.START_GAME).guard(context->{
return true;
})
.and()
.withExternal()
.source(States.NIGHT).target(States.DAY).event(Events.DAY_COMES)
.and()
.withExternal()
.source(States.DAY).target(States.NIGHT).event(Events.NIGHT_COMES);
}

@Bean
public StateMachine<States, Events> gameStateMachine() {
StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();
try {
builder.configureStates()
.withStates()
.initial(States.NIGHT)
.state(States.DAY);
builder.configureTransitions()
.withExternal()
.source(States.NIGHT).target(States.DAY).event(Events.DAY_COMES)
.and()
.withExternal()
.source(States.DAY).target(States.NIGHT).event(Events.NIGHT_COMES);
} catch (Exception e) {
e.printStackTrace();
}
return builder.build();
}

@Bean
public StateMachineListener<States, Events> listener() {
return new StateMachineListenerAdapter<States, Events>() {
@Override
public void stateChanged(State<States, Events> from, State<States, Events> to) {
System.out.println("State change from " + (from != null ? from.getId() : "null") + " to " + to.getId());
}
};
}
}

这里的守卫是org.springframework.statemachine.guard.Guard这个类,也可以使用spel表达式,将guard函数改为guardExpression即可