title: How can I configure Spring Batch StepScope using java-based configuration?
body: <p>I'm setting up a job server using Spring Batch.   My JdbcCursorItemReader needs to be configured with sql that changes on a per-job run basis.  Because the sql changes, I want the reader to have @StepScope so I don't need to worry about sql statefulness.</p>
  
  <p>So I've setup a class like this: </p>
  
  <pre><code>public class ParameterSettingJdbcCursorItemReader extends JdbcCursorItemReader implements StepExecutionListener {
  
      @Override
      public void beforeStep(StepExecution stepExecution) {
  
          JobParameters jobParameters = stepExecution.getJobParameters();
  
          if (jobParameters != null) {
              List&lt;Object&gt; args = new ArrayList&lt;Object&gt;();
              for (JobParameter jobParameter : jobParameters.getParameters().values()) {
                  args.add(jobParameter.getValue());
              }
  
              Object[] arrayArgs = args.toArray(new Object[args.size()]);
              String sql = String.format(getSql(), arrayArgs);
              setSql(sql);
          }
      }
  
      @Override
      public ExitStatus afterStep(StepExecution stepExecution) {
          return null;
      }
  }
  </code></pre>
  
  <p>The idea is that I pass sql parameters to the object via JobParameters and use String.format to fill in the dynamically-changing sql.</p>
  
  <p>I am using Java-based configuration throughout my server.  My bean for one instance of my ItemReader looks like:</p>
  
  <pre><code>@Bean
  @StepScope
  public ItemReader&lt;MyInputObject&gt; myInputObjectItemReader() {
      ParameterSettingJdbcCursorItemReader itemReader = new ParameterSettingJdbcCursorItemReader();
      itemReader.setDataSource(myDataSource());
      itemReader.setSql("SELECT * FROM my_table WHERE date = '%1$s'");
      itemReader.setRowMapper(myInputObjectMapper);
      return itemReader;
  }
  </code></pre>
  
  <p>When I start up my server and run the Spring Batch job, I get this error: <code>java.lang.IllegalStateException: No Scope registered for scope 'step'</code></p>
  
  <p>I've read elsewhere that in order to be able to use StepScope, one needs to first add it to the xml app configuration like this : 
  <code>&lt;bean class="org.springframework.batch.core.scope.StepScope" /&gt;</code></p>
  
  <p>But since I'm using Java-based configuration, that's not an option.  </p>
  
  <p>So how do I register the StepScope via a Java-based config?   I've tried this:</p>
  
  <pre><code>@Bean
  public org.springframework.batch.core.scope.StepScope stepScope() {
      return new org.springframework.batch.core.scope.StepScope();
  }
  </code></pre>
  
  <p>... but when I do I get all sorts of NPEs during app startup on beans that have no relation to StepScope.</p>
  
  <p>Thanks in advance.</p>