当前位置: 首页 > 知识库问答 >
问题:

使用spring-statemachine和spring cloud slueth时的BeanNotOfRequiredTypeException

纪俊良
2023-03-14

我目前正在使用Spring-Boot开发一个微服务。当同时使用spring-state machine和spring-cloud-sleuth工件时,我遇到了一个问题。

@Validated
@RestController
@SuppressWarnings({"squid:S00112"})
@RequestMapping()
public class StatusController {

@Autowired
private QuoteService quoteService;

@Autowired
private StateMachine<StateMachineDefinition.States, StateMachineDefinition.Events> stateMachine;

@Autowired
private QuoteStateHandler quoteStateHandler;


@Value("${StateMachine.InvalidField.message}")
private String statusInvalidField;

@Value("${StateMachine.QuoteCannotBeNull.message}")
private String quoteStatusNull;

private static final String STATUS = "STATUS";


@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
    binder.setAllowedFields("status");
}

/*
    Possible state transitions for the specific quote.
 */
@RequestMapping(method = RequestMethod.GET, value = {"/quotes/{quoteId}/transitions", "/{internalClient:(?:ui|s2s)}/{version:^[v]+[0-9]+$}/quotes/{quoteId}/transitions"})
public List<Status> getPossibleTransitions(@PathVariable("quoteId") String id) {

    String persistedStatus = quoteService.findOne(id).getStatus();


    if (persistedStatus == null) {
        persistedStatus = StateMachineDefinition.States.IN_PROCESS.name();
    }

    Collection<Transition<StateMachineDefinition.States, StateMachineDefinition.Events>> transitions = stateMachine.getTransitions();


    String currentState = persistedStatus;

    ArrayList<Status> possibleTransistions = new ArrayList<>();

    Iterator<Transition<StateMachineDefinition.States, StateMachineDefinition.Events>> iterator = transitions.iterator();
    String state;

    while (iterator.hasNext()) {

        Transition<StateMachineDefinition.States, StateMachineDefinition.Events> transition = iterator.next();
        state = transition.getSource().getId().name();

        if (state.compareTo(currentState) == 0) {
            possibleTransistions.add(new Status(transition.getTrigger().getEvent().toString()));

        }

    }

    return possibleTransistions;
}

@RequestMapping(method = RequestMethod.GET, value = {"/{internalClient:(?:ui)}/{version:^[v]+[0-9]+$}/states"})
public List<String> getStates() {

    Collection<State<StateMachineDefinition.States, StateMachineDefinition.Events>> states = stateMachine.getStates();
    Iterator<State<StateMachineDefinition.States, StateMachineDefinition.Events>> iterator = states.iterator();
    List<String> stateList = new ArrayList<>();

    while (iterator.hasNext()) {

        State<StateMachineDefinition.States, StateMachineDefinition.Events> state = iterator.next();
        stateList.add(state.getId().toString());
    }
    return stateList;
}

/*
Status is not a state but a transition or event.
 */
@RequestMapping(method = RequestMethod.POST, value = {"{quoteId}/transitions", "/{internalClient:(?:ui|s2s)}/{version:^[v]+[0-9]+$}/quotes/{quoteId}/transitions"})
@ResponseStatus(HttpStatus.NO_CONTENT)
public void postStatus(@RequestBody @Validated(Groups.Api.class) Status status, @PathVariable("quoteId") @Pattern(regexp = Patterns.UUID) String id, BindingResult bindingResult) throws Exception {


    if (bindingResult.hasErrors()) {

        throw new BadRequestValidationException(STATUS, statusInvalidField);

    }

    //get quoteid current status
    Quote currentQuote = quoteService.findOne(id);
    if (currentQuote.getStatus() != null) {

        StateMachineDefinition.States currentQuoteState = StateMachineDefinition.States.valueOf(currentQuote.getStatus());

        //need to send the event and let the state listener evaluate.
        if (status.getStatus() != null) {

            quoteStateHandler.handleEvent(
                    MessageBuilder
                            .withPayload(StateMachineDefinition.Events.valueOf(status.getStatus()))
                            .setHeader("quote-id", id)
                            .build(), currentQuoteState);

        }

        if (stateMachine.getExtendedState().getVariables().containsKey("ERROR")) {
            Exception exception = (Exception) stateMachine.getExtendedState().getVariables().get("ERROR");
            stateMachine.getExtendedState().getVariables().clear();
            throw exception;
        }

        if (stateMachine.getState().getId() != currentQuoteState) {
            quoteService.updateStatus(id, stateMachine.getState().getId());
        }

    } else {
        //If a quote has null status then it wasnt created properly.
        throw new BadRequestValidationException(STATUS, quoteStatusNull);
    }


}

}

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stateMachine': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'stateMachineTaskExecutor' is expected to be of type [org.springframework.core.task.TaskExecutor] but was actually of type [org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 44 common frames omitted
   <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth</artifactId>
            <version>1.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependency>
        <groupId>org.springframework.statemachine</groupId>
        <artifactId>spring-statemachine-core</artifactId>
        <version>LATEST</version>
<dependency>

我应该如何让spring应用程序上下文知道它必须加载哪种类型?因为这两个类都使用来自java util包的相同执行器。

ecutor.java.util.concurrent.executor

共有1个答案

林冥夜
2023-03-14

正如在1.0.12、1.1.1和1.2.0中修正的注释所示

 类似资料:
  • Spring Statemachine (Spring 状态机)是使用 Spring 框架下的状态机概念创建的一种应用程序开发框架。它使得状态机结构层次化,简化了配置状态机的过程。 详细功能: Easy to use flat one level state machine for simple use cases. Hierarchical state machine structure to

  • 我不确定这是否是问这个问题的正确地点,但我在spring-statemachine项目(http://projects.spring.io/spring-statemachine/)中看不到向开发人员提问的论坛链接,我希望这是正确的方法。 你可以在这里看到。 UML模型(不幸的是堆栈溢出不允许我发布额外的链接,所以请在前一个链接的末尾使用“#sm_model”) Eclipse M2T UML模型

  • 我开始使用Spring Statemachine,但在管理对象的状态时遇到了一些麻烦。 null 你觉得我的做法怎么样?

  • 我正试图让spring statemachine data jpa示例工作,但我无法。我在这里创建了一个示例github项目。到目前为止,我只添加了必要的依赖项,并得到了以下错误: 由:org.springframework.beans.factory.nosuchBeanDefinitionException引起:没有类型为'org.springframework.stateMachine.da

  • Short:有没有可能在纸莎草中创建层次结构(嵌套的)statemachine UML图,然后用它来构建工作的statemachine实例? 在我的主计算机中创建状态并将另一个子计算机链接到它(属性->submachine) 在单个状态机中创建所有内容,用子状态创建状态 在这两种情况下,创建的Spring statemachine都不能识别子机状态。 当我用以下代码列出应用程序中的所有状态时: