在将带有持久配置的弹簧状态机与@EnableJpaRepositorys DB配置一起使用时,我遇到了一个问题
Spring State Machine配置如下:(我有两个弹簧状态机,例如: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);
}
}
DB配置:
@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
您可以尝试的几件事:
1-将此配置包含到您的项目中
spring.datasource.main.allow-bean-definition-overriding: true
2-如果你的存储库和状态机转发之间有冲突,你可以映射要扫描的包
@EntityScan(basePackages = "your.entities.package")
@EnableJpaRepositories(basePackages = "your.repositories.package")