Fix mixup of builder configurers
This commit is contained in:
@@ -65,7 +65,7 @@ public abstract class AbstractImportingAnnotationConfiguration<B extends Annotat
|
||||
|
||||
BeanDefinition beanDefinition;
|
||||
try {
|
||||
beanDefinition = buildBeanDefinition();
|
||||
beanDefinition = buildBeanDefinition(importingClassMetadata);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error with onConfigurers", e);
|
||||
}
|
||||
@@ -83,63 +83,6 @@ public abstract class AbstractImportingAnnotationConfiguration<B extends Annotat
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract static class BeanDelegatingFactoryBean<B extends AnnotationBuilder<O>, O>
|
||||
implements FactoryBean<O>, InitializingBean {
|
||||
|
||||
private final B builder;
|
||||
|
||||
private O object;
|
||||
|
||||
private List<AnnotationConfigurer<O, B>> configurers;
|
||||
|
||||
public BeanDelegatingFactoryBean(B builder){
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract Class<O> getObjectType();
|
||||
|
||||
@Override
|
||||
public O getObject() throws Exception {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void afterPropertiesSet() throws Exception {
|
||||
// for (AnnotationConfigurer<O, B> configurer : configurers) {
|
||||
// if (configurer.isAssignable(builder)) {
|
||||
// // we need builder.apply(configurer);
|
||||
//// builder.
|
||||
// }
|
||||
// }
|
||||
// // should be getOrBuild???
|
||||
// object = builder.build();
|
||||
// }
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setConfigurers(List<AnnotationConfigurer<O, B>> configurers) {
|
||||
this.configurers = configurers;
|
||||
}
|
||||
|
||||
public B getBuilder() {
|
||||
return builder;
|
||||
}
|
||||
|
||||
public List<AnnotationConfigurer<O, B>> getConfigurers() {
|
||||
return configurers;
|
||||
}
|
||||
|
||||
protected void setObject(O object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
Assert.isInstanceOf(ListableBeanFactory.class, beanFactory,
|
||||
@@ -158,7 +101,7 @@ public abstract class AbstractImportingAnnotationConfiguration<B extends Annotat
|
||||
* @return the bean definition to register
|
||||
* @throws Exception if error occurred
|
||||
*/
|
||||
protected abstract BeanDefinition buildBeanDefinition() throws Exception;
|
||||
protected abstract BeanDefinition buildBeanDefinition(AnnotationMetadata importingClassMetadata) throws Exception;
|
||||
|
||||
/**
|
||||
* Gets the annotation specific for this configurer.
|
||||
@@ -185,4 +128,65 @@ public abstract class AbstractImportingAnnotationConfiguration<B extends Annotat
|
||||
return environment;
|
||||
}
|
||||
|
||||
protected abstract static class BeanDelegatingFactoryBean<T, B extends AnnotationBuilder<O>, O> implements
|
||||
FactoryBean<T>, BeanFactoryAware, InitializingBean {
|
||||
|
||||
private final B builder;
|
||||
|
||||
private T object;
|
||||
|
||||
private List<AnnotationConfigurer<O, B>> configurers;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private Class<T> clazz;
|
||||
|
||||
public BeanDelegatingFactoryBean(B builder, Class<T> clazz) {
|
||||
this.builder = builder;
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getObject() throws Exception {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setConfigurers(List<AnnotationConfigurer<O, B>> configurers) {
|
||||
this.configurers = configurers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public B getBuilder() {
|
||||
return builder;
|
||||
}
|
||||
|
||||
public List<AnnotationConfigurer<O, B>> getConfigurers() {
|
||||
return configurers;
|
||||
}
|
||||
|
||||
protected void setObject(T object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
protected BeanFactory getBeanFactory() {
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.statemachine.config.configuration;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineFactory;
|
||||
@@ -27,12 +38,21 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
|
||||
AbstractImportingAnnotationConfiguration<StateMachineConfigBuilder<S, E>, StateMachineConfig<S, E>> {
|
||||
|
||||
private final StateMachineConfigBuilder<S, E> builder = new StateMachineConfigBuilder<S, E>();
|
||||
|
||||
|
||||
@Override
|
||||
protected BeanDefinition buildBeanDefinition() throws Exception {
|
||||
protected BeanDefinition buildBeanDefinition(AnnotationMetadata importingClassMetadata) throws Exception {
|
||||
|
||||
Class<?> annotationType = getAnnotation();
|
||||
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(
|
||||
annotationType.getName(), false));
|
||||
String[] names = attributes.getStringArray("name");
|
||||
|
||||
|
||||
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(StateMachineDelegatingFactoryBean.class);
|
||||
.rootBeanDefinition(StateMachineDelegatingFactoryBean2.class);
|
||||
beanDefinitionBuilder.addConstructorArgValue(builder);
|
||||
beanDefinitionBuilder.addConstructorArgValue(StateMachine.class);
|
||||
beanDefinitionBuilder.addConstructorArgValue(names);
|
||||
return beanDefinitionBuilder.getBeanDefinition();
|
||||
}
|
||||
|
||||
@@ -41,62 +61,35 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
|
||||
return EnableStateMachine.class;
|
||||
}
|
||||
|
||||
private static class StateMachineDelegatingFactoryBean<S extends Enum<S>, E extends Enum<E>> implements
|
||||
FactoryBean<StateMachine<S, E>>, BeanFactoryAware, InitializingBean {
|
||||
private static class StateMachineDelegatingFactoryBean2<S extends Enum<S>, E extends Enum<E>>
|
||||
extends BeanDelegatingFactoryBean<StateMachine<S, E>,StateMachineConfigBuilder<S, E>,StateMachineConfig<S, E>> {
|
||||
|
||||
private final StateMachineConfigBuilder<S, E> builder;
|
||||
|
||||
private List<AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>> configurers;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private StateMachine<S, E> stateMachine;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public StateMachineDelegatingFactoryBean(StateMachineConfigBuilder<S, E> builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StateMachine<S, E> getObject() throws Exception {
|
||||
return stateMachine;
|
||||
}
|
||||
private final String[] beanNames;
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return StateMachine.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
public StateMachineDelegatingFactoryBean2(StateMachineConfigBuilder<S, E> builder, Class<StateMachine<S, E>> clazz, String[] beanNames) {
|
||||
super(builder, clazz);
|
||||
this.beanNames = beanNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
for (AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer : configurers) {
|
||||
builder.apply(configurer);
|
||||
for (AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer : getConfigurers()) {
|
||||
Class<?> clazz = configurer.getClass();
|
||||
EnableStateMachine findAnnotation = AnnotationUtils.findAnnotation(clazz, EnableStateMachine.class);
|
||||
String[] annonames = findAnnotation.name();
|
||||
if (beanNames[0].equals(annonames[0])) {
|
||||
getBuilder().apply(configurer);
|
||||
}
|
||||
}
|
||||
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
|
||||
StateMachineConfig<S, E> stateMachineConfig = getBuilder().getOrBuild();
|
||||
StateMachineTransitions<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
|
||||
StateMachineStates<S, E> stateMachineStates = stateMachineConfig.getStates();
|
||||
EnumStateMachineFactory<S,E> stateMachineFactory = new EnumStateMachineFactory<S, E>(stateMachineTransitions, stateMachineStates);
|
||||
stateMachineFactory.setBeanFactory(beanFactory);
|
||||
stateMachine = stateMachineFactory.getStateMachine();
|
||||
EnumStateMachineFactory<S, E> stateMachineFactory = new EnumStateMachineFactory<S, E>(
|
||||
stateMachineTransitions, stateMachineStates);
|
||||
stateMachineFactory.setBeanFactory(getBeanFactory());
|
||||
setObject(stateMachineFactory.getStateMachine());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Autowired(required=false)
|
||||
protected void onConfigurers(
|
||||
List<AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>> configurers)
|
||||
throws Exception {
|
||||
this.configurers = configurers;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.statemachine.config.EnableStateMachineFactory;
|
||||
import org.springframework.statemachine.config.EnumStateMachineFactory;
|
||||
import org.springframework.statemachine.config.StateMachineConfig;
|
||||
@@ -27,9 +28,9 @@ public class StateMachineFactoryConfiguration<S extends Enum<S>, E extends Enum<
|
||||
AbstractImportingAnnotationConfiguration<StateMachineConfigBuilder<S, E>, StateMachineConfig<S, E>> {
|
||||
|
||||
private final StateMachineConfigBuilder<S, E> builder = new StateMachineConfigBuilder<S, E>();
|
||||
|
||||
|
||||
@Override
|
||||
protected BeanDefinition buildBeanDefinition() throws Exception {
|
||||
protected BeanDefinition buildBeanDefinition(AnnotationMetadata importingClassMetadata) throws Exception {
|
||||
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(StateMachineFactoryDelegatingFactoryBean.class);
|
||||
beanDefinitionBuilder.addConstructorArgValue(builder);
|
||||
@@ -45,18 +46,18 @@ public class StateMachineFactoryConfiguration<S extends Enum<S>, E extends Enum<
|
||||
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;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public StateMachineFactoryDelegatingFactoryBean(StateMachineConfigBuilder<S, E> builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public StateMachineFactory<S, E> getObject() throws Exception {
|
||||
return stateMachineFactory;
|
||||
@@ -96,7 +97,7 @@ public class StateMachineFactoryConfiguration<S extends Enum<S>, E extends Enum<
|
||||
throws Exception {
|
||||
this.configurers = configurers;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.statemachine.config.common.annotation.AbstractImportingAnnotationConfiguration;
|
||||
import org.springframework.statemachine.config.common.annotation.AnnotationConfigurer;
|
||||
|
||||
@@ -27,9 +28,9 @@ import org.springframework.statemachine.config.common.annotation.AnnotationConfi
|
||||
public class SimpleTestConfiguration2 extends AbstractImportingAnnotationConfiguration<SimpleTestConfigBuilder, SimpleTestConfig> {
|
||||
|
||||
private final SimpleTestConfigBuilder builder = new SimpleTestConfigBuilder();
|
||||
|
||||
|
||||
@Override
|
||||
protected BeanDefinition buildBeanDefinition() throws Exception {
|
||||
protected BeanDefinition buildBeanDefinition(AnnotationMetadata importingClassMetadata) throws Exception {
|
||||
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(SimpleTestConfigDelegatingFactoryBean.class);
|
||||
beanDefinitionBuilder.addConstructorArgValue(builder);
|
||||
@@ -41,17 +42,12 @@ public class SimpleTestConfiguration2 extends AbstractImportingAnnotationConfigu
|
||||
return EnableSimpleTest2.class;
|
||||
}
|
||||
|
||||
private static class SimpleTestConfigDelegatingFactoryBean extends BeanDelegatingFactoryBean<SimpleTestConfigBuilder, SimpleTestConfig> {
|
||||
|
||||
private static class SimpleTestConfigDelegatingFactoryBean extends BeanDelegatingFactoryBean<SimpleTestConfig, SimpleTestConfigBuilder, SimpleTestConfig> {
|
||||
|
||||
public SimpleTestConfigDelegatingFactoryBean(SimpleTestConfigBuilder builder) {
|
||||
super(builder);
|
||||
super(builder, SimpleTestConfig.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<SimpleTestConfig> getObjectType() {
|
||||
return SimpleTestConfig.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
for (AnnotationConfigurer<SimpleTestConfig, SimpleTestConfigBuilder> configurer : getConfigurers()) {
|
||||
@@ -61,7 +57,7 @@ public class SimpleTestConfiguration2 extends AbstractImportingAnnotationConfigu
|
||||
}
|
||||
setObject(getBuilder().getOrBuild());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user