title: Spring-batch @BeforeStep does not work with @StepScope
body: <p>I'm using Spring Batch version 2.2.4.RELEASE
  I tried to write a simple example with stateful ItemReader, ItemProcessor and ItemWriter beans.</p>
  
  <pre><code>public class StatefulItemReader implements ItemReader&lt;String&gt; {
  
      private List&lt;String&gt; list;
  
      @BeforeStep
      public void initializeState(StepExecution stepExecution) {
          this.list = new ArrayList&lt;&gt;();
      }
  
      @AfterStep
      public ExitStatus exploitState(StepExecution stepExecution) {
          System.out.println("******************************");
          System.out.println(" READING RESULTS : " + list.size());
  
          return stepExecution.getExitStatus();
      }
  
      @Override
      public String read() throws Exception {
          this.list.add("some stateful reading information");
          if (list.size() &lt; 10) {
              return "value " + list.size();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>In my integration test, I'm declaring my beans in an inner static java config class like the one below:</p>
  
  <pre><code>@ContextConfiguration
  @RunWith(SpringJUnit4ClassRunner.class)
  public class SingletonScopedTest {
  
      @Configuration
      @EnableBatchProcessing
      static class TestConfig {
          @Autowired
          private JobBuilderFactory jobBuilder;
          @Autowired
          private StepBuilderFactory stepBuilder;
  
          @Bean
          JobLauncherTestUtils jobLauncherTestUtils() {
              return new JobLauncherTestUtils();
          }
  
          @Bean
          public DataSource dataSource() {
              EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
              return embeddedDatabaseBuilder.addScript("classpath:org/springframework/batch/core/schema-drop-hsqldb.sql")
                      .addScript("classpath:org/springframework/batch/core/schema-hsqldb.sql")
                      .setType(EmbeddedDatabaseType.HSQL)
                      .build();
          }
  
          @Bean
          public Job jobUnderTest() {
              return jobBuilder.get("job-under-test")
                      .start(stepUnderTest())
                      .build();
          }
  
          @Bean
          public Step stepUnderTest() {
              return stepBuilder.get("step-under-test")
                      .&lt;String, String&gt;chunk(1)
                      .reader(reader())
                      .processor(processor())
                      .writer(writer())
                      .build();
          }
  
          @Bean
          public ItemReader&lt;String&gt; reader() {
              return new StatefulItemReader();
          }
  
          @Bean
          public ItemProcessor&lt;String, String&gt; processor() {
              return new StatefulItemProcessor();
          }
  
          @Bean
          public ItemWriter&lt;String&gt; writer() {
              return new StatefulItemWriter();
          }
      }
  
      @Autowired
      JobLauncherTestUtils jobLauncherTestUtils;
  
      @Test
      public void testStepExecution() {
          JobExecution jobExecution = jobLauncherTestUtils.launchStep("step-under-test");
  
          assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
      }
  }
  </code></pre>
  
  <p>This test passes.</p>
  
  <p>But as soon as I define my <em>StatefulItemReader</em> as a step scoped bean (which is better for a stateful reader), the "before step" code is no longer executed.</p>
  
  <pre><code>...
      @Bean
      @StepScope
      public ItemReader&lt;String&gt; reader() {
          return new StatefulItemReader();
      }
  ...
  </code></pre>
  
  <p>And I notice the same issue with processor and my writer beans.</p>
  
  <p>What's wrong with my code? Is it related to this resolved issue: <a href="https://jira.springsource.org/browse/BATCH-1230">https://jira.springsource.org/browse/BATCH-1230</a></p>
  
  <p>My whole Maven project with several JUnit tests can be found on GitHub: <a href="https://github.com/galak75/spring-batch-step-scope">https://github.com/galak75/spring-batch-step-scope</a></p>
  
  <p>Thank you in advance for your answers.</p>