Tune automatic configuration

- Add StateMachineCommonConfiguration for task
  executing and scheduling.
- Add StateMachineConfigurationImportSelector for
  context event configuration.
- Fixes #40
This commit is contained in:
Janne Valkealahti
2015-05-08 12:50:04 +01:00
parent 3aa53af67b
commit 44c6e53834
5 changed files with 104 additions and 19 deletions

View File

@@ -28,7 +28,9 @@ import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.common.annotation.EnableAnnotationConfiguration;
import org.springframework.statemachine.config.common.annotation.configuration.ObjectPostProcessorConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineAnnotationPostProcessorConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineCommonConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineConfigurationImportSelector;
/**
* Example annotation which imports @{@link Configuration}s.
@@ -40,7 +42,8 @@ import org.springframework.statemachine.config.configuration.StateMachineConfigu
@Target(ElementType.TYPE)
@Documented
@EnableAnnotationConfiguration
@Import({ StateMachineConfiguration.class, ObjectPostProcessorConfiguration.class,
@Import({ StateMachineConfigurationImportSelector.class, StateMachineCommonConfiguration.class,
StateMachineConfiguration.class, ObjectPostProcessorConfiguration.class,
StateMachineAnnotationPostProcessorConfiguration.class })
public @interface EnableStateMachine {

View File

@@ -27,6 +27,9 @@ import org.springframework.context.annotation.Import;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.common.annotation.EnableAnnotationConfiguration;
import org.springframework.statemachine.config.common.annotation.configuration.ObjectPostProcessorConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineAnnotationPostProcessorConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineCommonConfiguration;
import org.springframework.statemachine.config.configuration.StateMachineConfigurationImportSelector;
import org.springframework.statemachine.config.configuration.StateMachineFactoryConfiguration;
/**
@@ -39,7 +42,9 @@ import org.springframework.statemachine.config.configuration.StateMachineFactory
@Target(ElementType.TYPE)
@Documented
@EnableAnnotationConfiguration
@Import({StateMachineFactoryConfiguration.class,ObjectPostProcessorConfiguration.class})
@Import({ StateMachineConfigurationImportSelector.class, StateMachineCommonConfiguration.class,
StateMachineFactoryConfiguration.class, ObjectPostProcessorConfiguration.class,
StateMachineAnnotationPostProcessorConfiguration.class })
public @interface EnableStateMachineFactory {
/**

View File

@@ -0,0 +1,44 @@
/*
* 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 org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
/**
* Common configuration for statemachine.
*
* @author Janne Valkealahti
*
*/
@Configuration
public class StateMachineCommonConfiguration {
@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.util.Map;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
/**
* Spring {@link ImportSelector} choosing appropriate {@link Configuration}
* based on {@link EnableStateMachine} or {@link EnableStateMachineFactory} annotations.
*
* @author Janne Valkealahti
*
*/
public class StateMachineConfigurationImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
Map<String, Object> attrMap = importingClassMetadata.getAnnotationAttributes(EnableStateMachine.class.getName());
if (attrMap == null) {
attrMap = importingClassMetadata.getAnnotationAttributes(EnableStateMachineFactory.class.getName());
}
if (attrMap != null && AnnotationAttributes.fromMap(attrMap).getBoolean("contextEvents")) {
return new String[] { "org.springframework.statemachine.event.StateMachineEventPublisherConfiguration",
"org.springframework.statemachine.config.configuration.StateMachineCommonConfiguration" };
} else {
return new String[0];
}
}
}

View File

@@ -9,16 +9,10 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.statemachine.event.OnStateEntryEvent;
import org.springframework.statemachine.event.OnStateExitEvent;
import org.springframework.statemachine.event.StateMachineEvent;
import org.springframework.statemachine.event.StateMachineEventPublisherConfiguration;
@Configuration
public class CommonConfiguration {
@@ -26,19 +20,8 @@ public class CommonConfiguration {
private final static Log log = LogFactory.getLog(CommonConfiguration.class);
@Configuration
@Import(StateMachineEventPublisherConfiguration.class)
static class ApplicationConfig {
@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
@Bean
public TestEventListener testEventListener() {
return new TestEventListener();