使用spring-statemachine和spring cloud slueth时的BeanNotOfRequiredTypeException [英] BeanNotOfRequiredTypeException when using spring-statemachine and spring cloud slueth

查看:172
本文介绍了使用spring-statemachine和spring cloud slueth时的BeanNotOfRequiredTypeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用spring-boot开发微服务,目前在同时使用spring-state机器和spring-cloud-sleuth工件时遇到问题.

I am currently developing a microservice using spring-boot.I currently have an issue when using spring-state machine and spring-cloud-sleuth artifacts together.

@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全新安装"时弹出错误,我才出现任何问题.

i didn't had any issues until I added the dependency of spring-cloud sleuth and the error popped up when I started doing the "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:创建名称为"stateMachine"的bean时出错:调用init方法失败;嵌套的异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为"stateMachineTaskExecutor"的Bean的类型应为[org.springframework.core.task.TaskExecutor],但实际上是[org.springframework.cloud.cloud.sleuth.instrument .async.LazyTraceExecutor]

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

Here is the pom.xml file with the two dependencies

   <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>

由于两个类都使用java util包中的同一执行程序,因此我应该如何让spring应用程序上下文知道它必须专门加载哪种类型.

How should I let the spring application context know which type specifically it has to load?Since both the classes are using the same executor from java util package.

java.util.concurrent.Executor

java.util.concurrent.Executor

推荐答案

如注释中所述,它已在1.0.12和1.1.1和1.2.0中得到修复

As presented in the comments it got fixed in the 1.0.12 and 1.1.1 and 1.2.0

这篇关于使用spring-statemachine和spring cloud slueth时的BeanNotOfRequiredTypeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆