我正在尝试在项目中利用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一直在说“未找到'StateMachine'类型的bean。”这可能是我的罪魁祸首。问题?):

@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如果指向spring核心类,则表示框架版本混在一起。您可能要让statemachine pom解析某些版本,而您自己的构建会添加其他内容。

您的maven或gradle构建文件可能显示了为什么核心框架jar具有不同的版本。如果使用引导,那么您将获得正确的版本,如果没有引导,则需要手动管理版本。

关于java - Spring Statemachine Framework导致buildBeanDefinition失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45313305/

10-09 21:49