spring状态机-jparepositorystatemachine不是托管类型错误

5t7ly7z5  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(373)

在将spring状态机与@enablejparepositories db配置一起使用时,我遇到了一个带有持久化配置的问题
spring状态机配置如下:(我有两个spring状态机,例如:ordermachine、usermachine)

@Slf4j
@Configuration
@EnableStateMachineFactory(name = “ORDERMACHINE")
public class OrderMachineConfiguration extends StateMachineConfigurerAdapter<States, Events> {

    @Autowired
    @Qualifier("OrderMachinePersister")
    private StateMachineRuntimePersister<States, Events, String> stateMachineRuntimePersister;

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
        transitions

                .withExternal().source(States.NEW).target(States.PREORDER).event(Events.CREATE)
                .and()
                .withExternal().source(States.PREORDER).target(States.CANCELLED).event(Events.CANCELLED);
    }

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

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
        config
                .withPersistence()
                .runtimePersister(stateMachineRuntimePersister);
    }

    @Bean(name = "OrderMachineService")
    public StateMachineService<States, Events> stateMachineServiceOrder(
             StateMachineFactory<States, Events> stateMachineFactory,
             StateMachineRuntimePersister<States, Events, String> stateMachineRuntimePersister) {
        return new DefaultStateMachineService<States, Events>(stateMachineFactory, stateMachineRuntimePersister);
    }

    @Bean(name = "OrderMachinePersister")
    public StateMachineRuntimePersister<States, Events, String> stateMachineRuntimePersisterOrder(
            JpaStateMachineRepository jpaStateMachineRepository) {
        return new JpaPersistingStateMachineInterceptor<>(jpaStateMachineRepository);
    }
}

数据库配置:

@Slf4j
@Configuration
@ConfigurationProperties("spring.datasource")
@EnableJpaRepositories(basePackages = “”org.machine.repository", entityManagerFactoryRef = “stateMachineEntityManager”, transactionManagerRef = “stateMachineTransactionManager”)
public class StateMachineDBConfiguration  {

    @Bean
    public DataSource stateMachineDatasource() {

        Properties props = getProperties();
        return DataSourceBuilder.create()
                .url(env.getProperty(MACHINE_DATASOURCE_URL))
                .driverClassName(env.getProperty(SPRING_DATASOURCE_DRIVER_CLASS_NAME))
                .username(props.getProperty(MACHINE_DATASOURCE_USERNAME))
                .password(props.getProperty(MACHINE_DATASOURCE_PASSWORD))
                .build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean stateMachineEntityManager() {
        return getEntityManager(stateManagerDatasource(), “org.machine.model”);
    }

    @Bean
    public PlatformTransactionManager stateMachineTransactionManager() {
        return getTransactionManager(stateMachineEntityManager());
    }

}

依赖项:

<dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-core</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-recipes-common</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-data-jpa</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

但在上面的配置中,我遇到了错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaStateMachineRepository' defined in org.springframework.statemachine.data.jpa.JpaStateMachineRepository defined in @EnableJpaRepositories declared on StateMachineDBConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class org.springframework.statemachine.data.jpa.JpaRepositoryStateMachine

Caused by: java.lang.IllegalArgumentException: Not a managed type: class org.springframework.statemachine.data.jpa.JpaRepositoryStateMachine

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题