title: Spring state-machine: common transition
body: <p>Is there a way to configure common external transition for multiple events? 
  i.e. </p>
  
  <ol>
  <li>For all states X,Y,Z -> CANCELED on CANCEL event.
  Where <em>X,Y,Z - sources</em>, <em>CANCELED - target state</em></li>
  <li>For CANCELED state run common <strong>internal</strong> transition on <strong>all</strong> events</li>
  </ol>
  
  <p>I hame more than 20 states to apply this transition to and it is not convenient to manually configure all of them.</p>
  
  <p>I am using <code>EnumStateMachineConfigurerAdapter</code> and <code>@EnableStateMachineFactory</code> to configure state machine, my configuration looks like this</p>
  
  <pre><code>@Configuration
  @EnableStateMachineFactory
  public class ContentOnlyStateMachineConfig extends EnumStateMachineConfigurerAdapter&lt;TaskState, WorkflowEvent&gt; {
  
  private final Action&lt;TaskState, WorkflowEvent&gt; someAction;
  private final Action&lt;TaskState, WorkflowEvent&gt; anotherAction;
  
  @Autowired
  public ContentOnlyStateMachineConfig(
          final Action&lt;TaskState, WorkflowEvent&gt; someAction,
          final Action&lt;TaskState, WorkflowEvent&gt; anotherAction) {
      this.someAction = someAction;
      this.anotherAction = anotherAction;
  }
  
  
  
  
  @Override
  public void configure(final StateMachineStateConfigurer&lt;TaskState, WorkflowEvent&gt; states)
          throws Exception {
  
      states
              .withStates()
              .initial(TaskState.CONTENT)
              .end(TaskState.COMPLETE)
              .state(TaskState.CONTENT, someAction)
              .state(TaskState.COMPLETE, anotherAction);
  
  }
  
  @Override
  public void configure(
          final StateMachineTransitionConfigurer&lt;TaskState, WorkflowEvent&gt; transitions)
          throws Exception {
  
      transitions
              .withExternal()
              .source(TaskState.CONTENT).target(TaskState.SOME_STATE)
              .event(WorkflowEvent.EXCEPTION)
              .and()
              .withExternal()
              .source(TaskState.EXCEPTION).target(TaskState.CANCELED)
              .event(WorkflowEvent.CANCEL)
              .and()
              .withExternal()
              .source(TaskState.SOME_STATE_2).target(TaskState.SOME_STATE_1)
              .event(WorkflowEvent.SOME_EVENT_3)
              .and()
              .withExternal()
              .source(TaskState.SOME_STATE).target(TaskState.SOME_STATE_2)
              .event(WorkflowEvent.SOME_EVENT_2)
              .and()
              .withExternal()
              .source(TaskState.SOME_STATE).target(TaskState.COMPLETE)
              .event(WorkflowEvent.ADMIN_ACCEPT)
              .and()
              .withExternal()
              .source(TaskState.SOME_STATE).target(TaskState.CONTENT)
              .event(WorkflowEvent.SOME_EVENT);
  }
  }
  </code></pre>