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
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// do not continue without configurers, it would not work
|
||||
if (getConfigurers() == null || getConfigurers().size() == 0) {
|
||||
throw new BeanDefinitionStoreException(
|
||||
"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);
|
||||
}
|
||||
}
|
||||
AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer =
|
||||
(AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>) getBeanFactory().getBean(Class.forName(clazzName));
|
||||
getBuilder().apply(configurer);
|
||||
|
||||
StateMachineConfig<S, E> stateMachineConfig = getBuilder().getOrBuild();
|
||||
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
||||
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
|
||||
|
||||
@@ -116,7 +116,6 @@ public class StateMachineFactoryConfiguration<S, E> extends
|
||||
FactoryBean<StateMachineFactory<S, E>>, BeanFactoryAware, InitializingBean {
|
||||
|
||||
private final StateMachineConfigBuilder<S, E> builder;
|
||||
private List<AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>> configurers;
|
||||
private BeanFactory beanFactory;
|
||||
private StateMachineFactory<S, E> stateMachineFactory;
|
||||
private String clazzName;
|
||||
@@ -146,18 +145,10 @@ public class StateMachineFactoryConfiguration<S, E> extends
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// do not continue without configurers, it would not work
|
||||
if (configurers == null || configurers.size() == 0) {
|
||||
throw new BeanDefinitionStoreException(
|
||||
"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);
|
||||
}
|
||||
}
|
||||
AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer =
|
||||
(AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>) beanFactory.getBean(Class.forName(clazzName));
|
||||
builder.apply(configurer);
|
||||
|
||||
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
|
||||
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
||||
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
|
||||
@@ -188,13 +179,6 @@ public class StateMachineFactoryConfiguration<S, E> extends
|
||||
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.springframework.beans.factory.BeanCreationException;
|
||||
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.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -313,6 +315,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
|
||||
@EnableStateMachine
|
||||
@@ -918,4 +933,52 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
||||
.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