使用spring-statemachine和spring cloud slueth时BeanNotOfRequiredTypeException

时间:2017-01-09 15:53:32

标签: java spring spring-boot spring-statemachine spring-cloud-sleuth

我目前正在使用spring-boot开发一个微服务。当使用弹簧状态机和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侦探的依赖性并且当我开始执行&#34; mvn clean install&#34;时出现错误,我才遇到任何问题。

错误堆栈跟踪:

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:创建名称为&#39; stateMachine&#39;的init时出错:init方法的调用失败;嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean命名为&#39; stateMachineTaskExecutor&#39;预计是[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

1 个答案:

答案 0 :(得分:0)

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