微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

需要一个无法找到的“org.springframework.statemachine.service.DefaultStateMachineService”类型的bean

如何解决需要一个无法找到的“org.springframework.statemachine.service.DefaultStateMachineService”类型的bean

我发现在控制器变量注入之前没有调用 DefaultStateMachineservice

说明:

com.example.statemachineDemo.restController 中构造函数的参数 1 需要一个无法找到的类型为“org.springframework.statemachine.service.DefaultStateMachineservice”的 bean。

我得到了 IntelliJ 的类型警告。

为什么?

我的另一个 spring 框架实现不会喜欢这个。

我按照 https://docs.spring.io/spring-statemachine/docs/current/reference/#configuring-model 为 DefaultStateMachineservice 添加 bean

enter image description here


package com.example.statemachineDemo;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachinestateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.persist.StateMachineRuntimePersister;
import org.springframework.statemachine.service.DefaultStateMachineservice;
import org.springframework.statemachine.service.StateMachineservice;

import javax.management.timer.Timer;

@Slf4j
@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends StateMachineConfigurerAdapter<String,String> {

    @Bean
    public StateMachineservice<String,String> defaultStateMachineservice(StateMachineFactory<String,String> stateMachineFactory) {
        return new DefaultStateMachineservice<String,String>(stateMachineFactory);
    }

    @Override
    public void configure(StateMachinestateConfigurer<String,String> states)
            throws Exception {
        states
                .withStates()
                .initial("S1")
                .state("S2")
                .state("S3");

        MessageBuilder
                .withPayload("E1")
                .setHeader("foo","bar")
                .build().getHeaders().get("foo",String.class);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String,String> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source("S1").target("S2").event("E1")
                .and()
                .withExternal()
                .source("S1").target("S3").event("E2")
                .and()
                .withExternal()
                .source("S2").target("S3").event("E3")
                .and()
                .withInternal()
                .source("S2")
                .action(timerAction())
                .timer(Timer.ONE_SECOND)
                .and()
                .withInternal()
                .source("S3")
                .action(context -> {
                    log.info("{}",context.getTransition());
                });
    }

    @Bean
    public TimerAction timerAction() {
        return new TimerAction();
    }
}

package com.example.statemachineDemo;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.service.DefaultStateMachineservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@Slf4j
@RestController
@AllArgsConstructor
public class restController {


    StateMachineFactory<String,String> factory;
    DefaultStateMachineservice<String,String> stateMachineFactory;

    @GetMapping("test1")
    public Flux<String> test1() {

       val stateMachine = factory.getStateMachine("testingID");

        stateMachine.start();
        stateMachine.sendEvent("E1");
        log.info("{}","test1");
        log.info("{}",stateMachine);

        return Flux.just("");
    }


    @GetMapping("test2")
    public Flux<String> test2() {
        val stateMachine = factory.getStateMachine("testingID");

        stateMachine.sendEvent("E3");
        log.info("{}","test2");
        log.info("{}",stateMachine);
        return Flux.just("");
    }

    @GetMapping("test3")
    public Flux<String> test3() {
        log.info("{}","test3");
        log.info("{}",factory.getStateMachine("testingID"));


        return Flux.just("");
    }

}


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?