buildBeanDefinition 因 Spring 状态机框架而失败

buildBeanDefinition failing with Spring Statemachine Framework

我试图在我的项目中使用 Spring Statemachine 框架,但是我在编译时遇到构建 bean 定义错误。

2017-07-25 16:37:25.462:WARN:oejd.DeploymentManager:Scanner-1: Unable to reach node goal: started
java.lang.NoSuchMethodError: org.springframework.beans.factory.support.RootBeanDefinition.setTargetType(Lorg/springframework/core/ResolvableType;)V
    at org.springframework.statemachine.config.configuration.StateMachineConfiguration.buildBeanDefinition(StateMachineConfiguration.java:109)
    at org.springframework.statemachine.config.common.annotation.AbstractImportingAnnotationConfiguration.registerBeanDefinitions(AbstractImportingAnnotationConfiguration.java:92)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:352)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:143)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)

我在 Spring MVC 应用程序中与 step 7 一起关注。我没有使用 SpringBoot,但我认为我没有遗漏任何子文件。

这是我的 StateMachineConfig 文件代码:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

import java.util.EnumSet;


@Configuration
@EnableStateMachine
public class StateMachineConfig
        extends EnumStateMachineConfigurerAdapter<States, Events> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config)
            throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .listener(listener());
    }

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states)
            throws Exception {
        states
                .withStates()
                .initial(States.SI)
                .states(EnumSet.allOf(States.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(States.SI).target(States.S1).event(Events.E1)
                .and()
                .withExternal()
                .source(States.S1).target(States.S2).event(Events.E2);
    }

    @Bean
    public StateMachineListener<States, Events> listener() {
        return new StateMachineListenerAdapter<States, Events>() {
            @Override
            public void stateChanged(State<States, Events> from, State<States, Events> to) {
                System.out.println("State change to " + to.getId());
            }
        };
    }
}

这是我尝试在其中使用 StateMachine 的控制器(注意:我删除了 stateMachine 实例化上方的 @Autowired 标记,因为 intelliJ 一直在说 "No beans of 'StateMachine' type found"。这可能是我的罪魁祸首有问题吗?):

@Controller
@RequestMapping("/")
public class HomeController {

    private StateMachine<States, Events> stateMachine;

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
            stateMachine.sendEvent(Events.E1);
            return "redirect:/target/list";
    }
}

NoSuchMethodError if points to spring core 类 意味着存在框架版本的混淆。您可能让 statemachine pom 解析某些版本,而您自己的构建添加了其他内容。

您的 Maven 或 gradle 构建文件可能显示了核心框架 jar 具有不同版本的原因。如果使用 boot 那么你几乎可以得到正确的版本,没有它你需要手动管理版本。