0

我目前正在使用 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);
    }


}

}

在我添加 spring-cloud sleuth 的依赖项之前我没有任何问题,当我开始执行“mvn clean install”时弹出错误。

错误堆栈跟踪:

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

错误消息 org.springframework.beans.factory.BeanCreationException: Error Creating bean with name 'stateMachine': Invocation of init method failed; 嵌套异常是 org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为 'stateMachineTaskExecutor' 的 Bean 应该是 [org.springframework.core.task.TaskExecutor] 类型,但实际上是 [org.springframework.cloud.sleuth.instrument .async.LazyTraceExecutor]

这是具有两个依赖项的 pom.xml 文件

   <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 包中的相同执行器。

java.util.concurrent.Executor

4

1 回答 1

0

如评论中所述,它在 1.0.12 和 1.1.1 和 1.2.0 中得到修复

于 2017-01-11T19:25:23.017 回答