diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/annotation/WithStateMachine.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/annotation/WithStateMachine.java index 4d2df8a9..d706943e 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/annotation/WithStateMachine.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/annotation/WithStateMachine.java @@ -22,15 +22,30 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.context.annotation.Configuration; +import org.springframework.statemachine.StateMachineSystemConstants; import org.springframework.stereotype.Component; +/** + * Annotation which is marking a bean to be a candidate for participating with a + * state machine events. + * + * @author Janne Valkealahti + * + */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @Component +@Configuration public @interface WithStateMachine { - String name() default ""; - + /** + * The name of a state machine which annotated bean should be associated. + * + * @return the state machine bean name + */ + String name() default StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE; + } diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/EnableStateMachine.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/EnableStateMachine.java index 1f9da39f..89eab1de 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/EnableStateMachine.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/EnableStateMachine.java @@ -27,6 +27,7 @@ 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.StateMachineConfiguration; /** @@ -39,9 +40,10 @@ import org.springframework.statemachine.config.configuration.StateMachineConfigu @Target(ElementType.TYPE) @Documented @EnableAnnotationConfiguration -@Import({StateMachineConfiguration.class,ObjectPostProcessorConfiguration.class}) +@Import({ StateMachineConfiguration.class, ObjectPostProcessorConfiguration.class, + StateMachineAnnotationPostProcessorConfiguration.class }) public @interface EnableStateMachine { - + /** * The name of bean, or if plural, aliases for bean created based on this * annotation. If left unspecified bean name will be autogenerated. @@ -50,5 +52,5 @@ public @interface EnableStateMachine { * @return the array if names or empty as default */ String[] name() default {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE}; - + } \ No newline at end of file diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configuration/StateMachineAnnotationPostProcessorConfiguration.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configuration/StateMachineAnnotationPostProcessorConfiguration.java new file mode 100644 index 00000000..47cd03cc --- /dev/null +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configuration/StateMachineAnnotationPostProcessorConfiguration.java @@ -0,0 +1,40 @@ +/* + * 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.statemachine.annotation.WithStateMachine; +import org.springframework.statemachine.processor.StateMachineAnnotationPostProcessor; + +/** + * Configuration for annotation port processor which is needed i.e. when + * {@link WithStateMachine} is used. + * + * @author Janne Valkealahti + * + */ +@Configuration +public class StateMachineAnnotationPostProcessorConfiguration { + + private final static String POST_PROCESSOR_BEAN_ID = "org.springframework.statemachine.processor.stateMachineAnnotationPostProcessor"; + + @Bean(name = POST_PROCESSOR_BEAN_ID) + public StateMachineAnnotationPostProcessor springStateMachineAnnotationPostProcessor() { + return new StateMachineAnnotationPostProcessor(); + } + +} diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java index 61c2f7f4..dcc8eca2 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java @@ -37,14 +37,13 @@ import org.springframework.statemachine.config.EnableStateMachine; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; -import org.springframework.statemachine.processor.StateMachineAnnotationPostProcessor; public class MethodAnnotationTests extends AbstractStateMachineTests { @Test @SuppressWarnings("unchecked") public void testMethodAnnotations() throws Exception { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BaseConfig.class, AnnoConfig.class, BeanConfig1.class, Config1.class); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig1.class, Config1.class); EnumStateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class); @@ -65,7 +64,7 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { @Test @SuppressWarnings("unchecked") public void testMethodAnnotations2() throws Exception { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BaseConfig.class, AnnoConfig.class, BeanConfig2.class, Config1.class); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig2.class, Config1.class); EnumStateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class); @@ -90,7 +89,7 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { context.close(); } - @WithStateMachine(name = StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE) + @WithStateMachine static class Bean1 { CountDownLatch onMethod1Latch = new CountDownLatch(1); @@ -108,7 +107,7 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { } - @WithStateMachine(name = StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE) + @WithStateMachine static class Bean2 { CountDownLatch onMethod1Latch = new CountDownLatch(1); @@ -153,16 +152,6 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { } - @Configuration - static class AnnoConfig { - - @Bean(name="org.springframework.statemachine.internal.springStateMachineAnnotationPostProcessor") - public StateMachineAnnotationPostProcessor springStateMachineAnnotationPostProcessor() { - return new StateMachineAnnotationPostProcessor(); - } - - } - @Configuration @EnableStateMachine(name = {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, "fooMachine"}) static class Config1 extends EnumStateMachineConfigurerAdapter {