Java 将存储库或服务注入Spring statemachine Action和Guard的正确方法
我一直在研究Spring Statemachine,并用它对订单的状态进行建模。我读到关于Java 将存储库或服务注入Spring statemachine Action和Guard的正确方法,java,spring,spring-statemachine,Java,Spring,Spring Statemachine,我一直在研究Spring Statemachine,并用它对订单的状态进行建模。我读到关于警卫和行动,根据我们的要求,出现了一个问题 将Spring@存储库或@服务注入防护或操作的正确方法是什么 如中所述,Guards和Actions是通过声明@Bean来配置的,但我认为没有办法以这种方式注入@Service或@Repository 例如,以以下EnumStateMachineConfigureAdapter为例: @Configuration @EnableStateMachineFactor
警卫
和行动
,根据我们的要求,出现了一个问题
将Spring@存储库
或@服务
注入防护
或操作
的正确方法是什么
如中所述,Guard
s和Action
s是通过声明@Bean
来配置的,但我认为没有办法以这种方式注入@Service
或@Repository
例如,以以下EnumStateMachineConfigureAdapter
为例:
@Configuration
@EnableStateMachineFactory
public class OrderStateMachineConfiguration extends EnumStateMachineConfigurerAdapter<OrderStates, OrderEvents> {
@Override
public void configure(StateMachineStateConfigurer<OrderStates, OrderEvents> states) throws Exception {
states
.withStates()
.initial(OrderStates.PENDING)
.initial(OrderStates.APPROVED)
.initial(OrderStates.PAYMENT_REJECTED)
.end(OrderStates.DELIVERED)
.end(OrderStates.CANCELLED)
.end(OrderStates.INVALID)
.end(OrderStates.REFUNDED)
.end(OrderStates.ARCHIVED)
.states(EnumSet.allOf(OrderStates.class));
}
@Override
public void configure(StateMachineConfigurationConfigurer<OrderStates, OrderEvents> config) throws Exception {
config
.withConfiguration()
.listener(new StateMachineListener());
}
@Override
public void configure(StateMachineTransitionConfigurer<OrderStates, OrderEvents> transitions) throws Exception {
transitions
.withExternal().source(OrderStates.PENDING).target(OrderStates.APPROVED).event(OrderEvents.APPROVE).and()
... more transitions
}
@Bean
public Guard<OrderStates, OrderEvents> orderIsConsistent() {
return ctx -> {
Order order = ctx.getExtendedState().get(ORDER_KEY, Order.class);
return order.getInconsistencies().isEmpty();
};
}
}
@配置
@EnableStateMachineFactory
公共类OrderStateMachineConfiguration扩展了EnumStateMachineConfiguration适配器{
@凌驾
public void configure(StateMachineStateConfigurer状态)引发异常{
州
.with states()
.initial(OrderStates.PENDING)
.首字母(OrderState.APPROVED)
.首字母(订单状态。付款被拒绝)
.end(OrderStates.DELIVERED)
.end(OrderStates.CANCELLED)
.end(OrderStates.INVALID)
.end(订单状态。已退款)
.end(OrderStates.ARCHIVED)
.states(EnumSet.allOf(OrderStates.class));
}
@凌驾
public void configure(stateMachineConfiguration配置器配置)引发异常{
配置
.withConfiguration()
.listener(新StateMachineListener());
}
@凌驾
public void configure(StateMachineTransitionConfigurer转换)引发异常{
过渡
.withExternal().source(OrderStates.PENDING).target(OrderStates.APPROVED).事件(ordersevents.APPROVE).和()
…更多的转变
}
@豆子
公共警卫命令一致(){
返回ctx->{
Order Order=ctx.getExtendedState().get(Order\u键,Order.class);
退货订单.getInconsistences().isEmpty();
};
}
}
将@Autowired
服务放在顶部似乎是不对的,因为这是一个@Configuration
类,更不用说循环引用的风险了
我提出的另一个解决方案可能是在创建状态机时将所需的服务注入到extendedState
或状态机头中,然后通过StateContext
访问它
我希望能对此有所了解,因为我在文档中找不到答案。您可以在方法级别注入依赖项:
@Bean
@Autowired
public Guard<OrderStates, OrderEvents> orderIsConsistent(OrderService orderService) {
return ctx -> {
Long orderId = ctx.getExtendedState().get(ORDER_ID, Long.class);
Order order = orderService.findById(orderId);
return order.getInconsistencies().isEmpty();
};
}
@Bean
@自动连线
公共警卫命令一致(OrderService OrderService){
返回ctx->{
Long orderId=ctx.getExtendedState().get(ORDER\u ID,Long.class);
Order=orderService.findById(orderId);
退货订单.getInconsistences().isEmpty();
};
}
您可以在方法级别注入依赖项:
@Bean
@Autowired
public Guard<OrderStates, OrderEvents> orderIsConsistent(OrderService orderService) {
return ctx -> {
Long orderId = ctx.getExtendedState().get(ORDER_ID, Long.class);
Order order = orderService.findById(orderId);
return order.getInconsistencies().isEmpty();
};
}
@Bean
@自动连线
公共警卫命令一致(OrderService OrderService){
返回ctx->{
Long orderId=ctx.getExtendedState().get(ORDER\u ID,Long.class);
Order=orderService.findById(orderId);
退货订单.getInconsistences().isEmpty();
};
}