Lookup needed configurer instead of injecting all configurers
- Also lookup Configurer in StateMachineConfiguration - Added test scenario for constructor injection issue - Fixes #522
This commit is contained in:
committed by
Janne Valkealahti
parent
9788d2dfeb
commit
dd8c4ea665
@@ -155,18 +155,10 @@ public class StateMachineConfiguration<S, E> extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
// do not continue without configurers, it would not work
|
AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer =
|
||||||
if (getConfigurers() == null || getConfigurers().size() == 0) {
|
(AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>) getBeanFactory().getBean(Class.forName(clazzName));
|
||||||
throw new BeanDefinitionStoreException(
|
getBuilder().apply(configurer);
|
||||||
"Cannot configure state machine due to missing configurers. Did you remember to use " +
|
|
||||||
"@EnableStateMachine with a StateMachineConfigurerAdapter.");
|
|
||||||
}
|
|
||||||
for (AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer : getConfigurers()) {
|
|
||||||
Class<?> clazz = configurer.getClass();
|
|
||||||
if (ClassUtils.getUserClass(clazz).getName().equals(clazzName)) {
|
|
||||||
getBuilder().apply(configurer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StateMachineConfig<S, E> stateMachineConfig = getBuilder().getOrBuild();
|
StateMachineConfig<S, E> stateMachineConfig = getBuilder().getOrBuild();
|
||||||
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
||||||
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
|
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ public class StateMachineFactoryConfiguration<S, E> extends
|
|||||||
FactoryBean<StateMachineFactory<S, E>>, BeanFactoryAware, InitializingBean {
|
FactoryBean<StateMachineFactory<S, E>>, BeanFactoryAware, InitializingBean {
|
||||||
|
|
||||||
private final StateMachineConfigBuilder<S, E> builder;
|
private final StateMachineConfigBuilder<S, E> builder;
|
||||||
private List<AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>> configurers;
|
|
||||||
private BeanFactory beanFactory;
|
private BeanFactory beanFactory;
|
||||||
private StateMachineFactory<S, E> stateMachineFactory;
|
private StateMachineFactory<S, E> stateMachineFactory;
|
||||||
private String clazzName;
|
private String clazzName;
|
||||||
@@ -146,18 +145,10 @@ public class StateMachineFactoryConfiguration<S, E> extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
// do not continue without configurers, it would not work
|
AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer =
|
||||||
if (configurers == null || configurers.size() == 0) {
|
(AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>) beanFactory.getBean(Class.forName(clazzName));
|
||||||
throw new BeanDefinitionStoreException(
|
builder.apply(configurer);
|
||||||
"Cannot configure state machine due to missing configurers. Did you remember to use " +
|
|
||||||
"@EnableStateMachineFactory with a StateMachineConfigurerAdapter.");
|
|
||||||
}
|
|
||||||
for (AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer : configurers) {
|
|
||||||
Class<?> clazz = configurer.getClass();
|
|
||||||
if (ClassUtils.getUserClass(clazz).getName().equals(clazzName)) {
|
|
||||||
builder.apply(configurer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
|
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
|
||||||
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
||||||
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
|
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
|
||||||
@@ -188,13 +179,6 @@ public class StateMachineFactoryConfiguration<S, E> extends
|
|||||||
this.beanFactory = beanFactory;
|
this.beanFactory = beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired(required=false)
|
|
||||||
protected void onConfigurers(
|
|
||||||
List<AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>> configurers)
|
|
||||||
throws Exception {
|
|
||||||
this.configurers = configurers;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ import java.util.List;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -314,6 +316,19 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMachinesWithDependenciesAndConstructorInjection() {
|
||||||
|
context.register(Config21.class);
|
||||||
|
context.register(Config22.class);
|
||||||
|
context.refresh();
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
StateMachineFactory<String, String> stateMachineFactory22 = context.getBean("stateMachineConfig22", StateMachineFactory.class);
|
||||||
|
StateMachine<String,String> stateMachine22 = stateMachineFactory22.getStateMachine();
|
||||||
|
assertThat(stateMachine22, notNullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableStateMachine
|
@EnableStateMachine
|
||||||
public static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
public static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||||
@@ -918,4 +933,52 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
|||||||
.event("E1");
|
.event("E1");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableStateMachineFactory(name="stateMachineConfig21")
|
||||||
|
public static class Config21 extends StateMachineConfigurerAdapter<String, String> {
|
||||||
|
@Override
|
||||||
|
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
|
||||||
|
states
|
||||||
|
.withStates()
|
||||||
|
.initial("S1")
|
||||||
|
.end("S2")
|
||||||
|
.end("S3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
|
||||||
|
transitions
|
||||||
|
.withExternal()
|
||||||
|
.source("S1")
|
||||||
|
.target("S2")
|
||||||
|
.event("E1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableStateMachineFactory(name="stateMachineConfig22")
|
||||||
|
public static class Config22 extends StateMachineConfigurerAdapter<String, String> {
|
||||||
|
@Autowired
|
||||||
|
Config22(@Qualifier("stateMachineConfig21") StateMachineFactory<String, String> otherStateMachine) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
|
||||||
|
states
|
||||||
|
.withStates()
|
||||||
|
.initial("S1")
|
||||||
|
.end("S2")
|
||||||
|
.end("S3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
|
||||||
|
transitions
|
||||||
|
.withExternal()
|
||||||
|
.source("S1")
|
||||||
|
.target("S2")
|
||||||
|
.event("E1");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user