title: How to autowire beans inside a Spring Integration custom message handler?
body: <p>I wish create a custom message handler to use checkpoints in flows. Besides, those checkpoints will be stored in <strong>ElasticSearch</strong>.</p>
  
  <p>I created a class <strong>Checkpoint</strong>:</p>
  
  <pre><code>@Component
  public class Checkpoint {
  
      public static final String TASK_HEADER_KEY = "task";
  
      public static CheckpointMessageHandlerSpec warn(String message) {
          return new CheckpointMessageHandlerSpec(new CheckpointHandler("WARN", message));
      }
  }
  // ... methods omitted: error, info etc
  </code></pre>
  
  <p>Next I created <strong>CheckpointMessageHandlerSpec</strong>:</p>
  
  <pre><code>public class CheckpointMessageHandlerSpec extends MessageHandlerSpec&lt;CheckpointMessageHandlerSpec, CheckpointHandler&gt; {
  
      public CheckpointMessageHandlerSpec(CheckpointHandler checkpointHandler) {
          this.target = checkpointHandler;
      }
  
      public CheckpointMessageHandlerSpec apply(Message&lt;?&gt; message) {
          this.target.handleMessage(message);
          return _this();
      }
  
      @Override
      protected CheckpointHandler doGet() {
          throw new UnsupportedOperationException();
      }
  }
  </code></pre>
  
  <p><strong>CheckpointHandler</strong>, in this class I wish to inject things, like services or repositories from Spring Data:</p>
  
  <pre><code>public class CheckpointHandler extends IntegrationObjectSupport implements MessageHandler {
  
      private String status;
      private String message;
  
      // I want inject services or repositories here
  
      public CheckpointHandler(String status, String message) {
          this.status = status;
          this.message = message;
      }
  
      @Override
      public void handleMessage(Message&lt;?&gt; message) {
          // Test to watch if I have the bean factory. It is always null
          this.getBeanFactory();
  
          Expression expression = EXPRESSION_PARSER.parseExpression("'" + this.message + "'");
  
          // Here I intend to persist information of payload/headers with spring-data-elasticSearch repository previously injected   
          Object obj = expression.getValue(message);
      }
  }
  </code></pre>
  
  <p>Finally, a example of use, inside a flow:</p>
  
  <pre><code>@Bean
  public IntegrationFlow checkpointFlow(Checkpoint checkpoint) {
      return IntegrationFlows.from(Http.inboundChannelAdapter("/checkpointFlow"))
              .enrichHeaders(Collections.singletonMap(Checkpoint.TASK_HEADER_KEY, taskName))
              .handle(new AppendMessageHandler())
              .wireTap(c -&gt; c.handle(m -&gt; checkpoint.warn("SOMETHING IS HAPPENING HERE. MY PAYLOAD: ' + payload.toString() + '").apply(m)))
              .handle(m -&gt; log.info("[LOGGING DEMO] {}" , m.getPayload()))
              .get();
  }
  
  private class AppendMessageHandler implements GenericHandler {
  
      @Override
      public String handle(Object payload, Map headers) {
          return new StringBuilder().append(testMessage).toString();
      }
  }
  </code></pre>
  
  <p>What I miss? Is it possible to do that? I had this idea following this question <a href="https://stackoverflow.com/questions/31626497/how-to-create-custom-component-and-add-it-to-flow-in-spring-java-dsl">How to create custom component and add it to flow in spring java dsl?</a></p>
  
  <p>Thanks!</p>