Tweak how annotation processor is enabled

- StateMachineAnnotationPostProcessor is now in its own
  StateMachineAnnotationPostProcessorConfiguration which gets
  imported automatically from @EnableStateMachine.
This commit is contained in:
Janne Valkealahti
2015-03-08 09:19:13 +00:00
parent fe0f7ebb95
commit c2d106c617
4 changed files with 66 additions and 20 deletions

View File

@@ -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;
}

View File

@@ -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};
}

View File

@@ -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();
}
}

View File

@@ -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<TestStates,TestEvents> 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<TestStates,TestEvents> 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<TestStates, TestEvents> {