title: Spring batch scope issue while using spring boot
body: <p>I'm having Standalone spring batch job. This works perfectly fine when in JUNIT</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  //@SpringApplicationConfiguration(classes = KPBootApplication.class)
  @ContextConfiguration(locations={"classpath:kp-sb.xml"})
  public class KPBootApplicationTests {
  
      private final static Logger LOG=LoggerFactory.getLogger(KPBootApplicationTests.class);
  
      @Autowired
      ApplicationContext context;
  
      @Autowired
      private JobLauncher jobLauncher;
  
      @Autowired
      private Job job;
  
      @Test
      public void testJob() {
  
          final JobParameters jobParameters = new JobParametersBuilder()
                  .toJobParameters();
  
          JobExecution execution;
          try {
              execution = jobLauncher.run(job, jobParameters);
              final ExitStatus status = execution.getExitStatus();
  
              if (ExitStatus.COMPLETED.getExitCode().equals(status.getExitCode())) {
                  LOG.info("Job completed Yeaaaaaaaa!!!!!");
              } else {
                  final List&lt;Throwable&gt; exceptions = execution
                          .getAllFailureExceptions();
                  for (final Throwable throwable : exceptions) {
                      LOG.error(throwable.getMessage(), throwable);
  
                  }
              }
          } catch (JobExecutionAlreadyRunningException e) {
              LOG.error(e.getMessage(), e);
          } catch (JobRestartException e) {
              LOG.error(e.getMessage(), e);
          } catch (JobInstanceAlreadyCompleteException e) {
              LOG.error(e.getMessage(), e);
          } catch (JobParametersInvalidException e) {
              LOG.error(e.getMessage(), e);
          }
  
      }
  
  }
  </code></pre>
  
  <p>And the configuration file</p>
  
  <pre><code>&lt;!-- Below code till Job Repo is commented out during spring-boot --&gt;
  &lt;context:property-placeholder
      properties-ref="kpProps" /&gt;
  &lt;util:properties id="kpProps"&gt;
      &lt;prop key="app.file.path"&gt;
          D:/temp/kp1/all
      &lt;/prop&gt;
      &lt;prop key="app.max_thread_num"&gt;
          10
      &lt;/prop&gt;
  &lt;/util:properties&gt;
  
  &lt;!-- 
  &lt;bean id="jobLauncher"
      class="org.springframework.batch.core.launch.support.SimpleJobLauncher"&gt;
      &lt;property name="jobRepository" ref="jobRepository" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jobRepository"
      class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"&gt;
      &lt;property name="transactionManager" ref="batchTransactionManager" /&gt;
  &lt;/bean&gt;
   --&gt;
  
  &lt;bean id="batchTransactionManager"
      class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"&gt;
      &lt;property name="rollbackOnCommitFailure" value="false" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="multiResourcePartitionerReq"
      class="org.springframework.batch.core.partition.support.MultiResourcePartitioner"&gt;
      &lt;property name="resources" value="file:${app.file.path}/kp_http_request*" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="multiResourcePartitionerRes"
      class="org.springframework.batch.core.partition.support.MultiResourcePartitioner"&gt;
      &lt;property name="resources" value="file:${app.file.path}/kp_http_response*" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="kpPool"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
      destroy-method="destroy"&gt;
      &lt;property name="maxPoolSize" value="${app.max_thread_num}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="idIncrementer1"
      class="org.springframework.batch.core.launch.support.RunIdIncrementer" /&gt;
  
  &lt;batch:job id="kpGenJob" incrementer="idIncrementer1"
      job-repository="jobRepository"&gt;
      &lt;batch:split id="splitStep" next="step4"&gt;
          &lt;batch:flow&gt;
              &lt;batch:step id="step2"&gt;
                  &lt;batch:partition partitioner="multiResourcePartitionerReq"
                      step="step2.slave"&gt;
                      &lt;batch:handler task-executor="kpPool" /&gt;
                  &lt;/batch:partition&gt;
              &lt;/batch:step&gt;
          &lt;/batch:flow&gt;
          &lt;batch:flow&gt;
              &lt;batch:step id="step3"&gt;
                  &lt;batch:partition partitioner="multiResourcePartitionerRes"
                      step="step3.slave"&gt;
                      &lt;batch:handler task-executor="kpPool" /&gt;
                  &lt;/batch:partition&gt;
              &lt;/batch:step&gt;
          &lt;/batch:flow&gt;
      &lt;/batch:split&gt;
      &lt;batch:step id="step4"&gt;
          &lt;batch:tasklet transaction-manager="batchTransactionManager"&gt;
              &lt;ref bean="kptasklet" /&gt;
          &lt;/batch:tasklet&gt;
      &lt;/batch:step&gt;
  &lt;/batch:job&gt;
  
  &lt;batch:step id="step2.slave"&gt;
      &lt;batch:tasklet transaction-manager="batchTransactionManager"&gt;
          &lt;batch:chunk reader="reqItemReader" writer="cvsFileItemWriter"
              commit-interval="10000" /&gt;
      &lt;/batch:tasklet&gt;
  &lt;/batch:step&gt;
  
  &lt;batch:step id="step3.slave"&gt;
      &lt;batch:tasklet transaction-manager="batchTransactionManager"&gt;
          &lt;batch:chunk reader="resItemReader" writer="cvsFileItemWriter"
              commit-interval="10000" /&gt;
      &lt;/batch:tasklet&gt;
  &lt;/batch:step&gt;
  
  &lt;bean id="reqItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
      scope="step"&gt;
  
      &lt;property name="resource" value="#{stepExecutionContext['fileName']}" /&gt;
  
      &lt;property name="lineMapper"&gt;
          &lt;bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"&gt;
              &lt;!-- split it --&gt;
              &lt;property name="lineTokenizer"&gt;
                  &lt;bean
                      class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"&gt;
                      &lt;property name="includedFields" value="5,6,8,10,11"&gt;&lt;/property&gt;
                      &lt;property name="names"
                          value="f1,f2,f3,f4,f5" /&gt;
                      &lt;property name="strict" value="false" /&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
              &lt;property name="fieldSetMapper"&gt;
                  &lt;bean
                      class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"&gt;
                      &lt;property name="prototypeBeanName" value="tblHttpData" /&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="linesToSkip" value="1"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="tblHttpData" class="com.kp.batch.batch.job.domain.TblHttpData"
      scope="prototype" /&gt;
  
  &lt;bean id="resItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
      scope="step"&gt;
  
      &lt;property name="resource" value="#{stepExecutionContext['fileName']}" /&gt;
  
      &lt;property name="lineMapper"&gt;
          &lt;bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"&gt;
              &lt;!-- split it --&gt;
              &lt;property name="lineTokenizer"&gt;
                  &lt;bean
                      class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"&gt;
                      &lt;property name="includedFields" value="3,4"&gt;&lt;/property&gt;
                      &lt;property name="names" value="f1,f2" /&gt;
                      &lt;property name="strict" value="false" /&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
              &lt;property name="fieldSetMapper"&gt;
  
                  &lt;bean
                      class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"&gt;
                      &lt;property name="prototypeBeanName" value="tblHttpData" /&gt;
                      &lt;property name="strict" value="true" /&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="linesToSkip" value="1"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="kptasklet" class="com.kp.batch.batch.job.step.KPTasklet" /&gt;
  
  &lt;bean id="cvsFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
      scope="step"&gt;
      &lt;!-- write to this csv file --&gt;
      &lt;property name="resource" value="#{stepExecutionContext['fileName']}.tmp" /&gt;
      &lt;property name="shouldDeleteIfExists" value="true" /&gt;
  
      &lt;property name="lineAggregator"&gt;
          &lt;bean
              class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"&gt;
              &lt;property name="delimiter" value="," /&gt;
  
              &lt;property name="fieldExtractor"&gt;
                  &lt;bean
                      class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"&gt;
                      &lt;property name="names"
                          value="server,refferer,application,baseApplication,httpSize" /&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>The Main Class</strong></p>
  
  <pre><code>@SpringBootApplication
  @EnableBatchProcessing
  @ImportResource(value={"classpath:spring-context.xml"})
  public class KPBootApplication {
  
      private final static Logger LOG = LoggerFactory
              .getLogger(KPBootApplication.class);
  
      public static void main(String[] args) {
          ApplicationContext ctx = SpringApplication.run(
                  KPBootApplication.class, args);
          LOG.info("Application KPBOOT Started");
          SayHello hello = (SayHello) ctx.getBean("sayHello");
          if (hello != null) {
              LOG.debug("hello is not null");
              LOG.info("Got message {}", hello.getMessage());
          } else {
              LOG.debug("hello is null");
          }
          LOG.info("Done");
  
      }
  }
  </code></pre>
  
  <p>When I comment out contextconfiguration annotation and enable spring-boot by enabling @SpringApplicationConfiguration( note: I've enabled spring batch using annotation @EnableBatchProcessing) and comment out beans which are automcatically created by spring-boot for spring batch such as JobRepo I get following error saying no context holder for step scope</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
      at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step2': Cannot resolve reference to bean 'step2.slave' while setting bean property 'step'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step2.slave': Cannot resolve reference to bean 'reqItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reqItemReader': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:743)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
      at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:98)
      at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
      at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
      ... 25 common frames omitted
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step2.slave': Cannot resolve reference to bean 'reqItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reqItemReader': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
      ... 42 common frames omitted
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reqItemReader': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:352)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
      ... 52 common frames omitted
  Caused by: java.lang.IllegalStateException: No context holder available for step scope
      at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:160)
      at org.springframework.batch.core.scope.StepScope.get(StepScope.java:99)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
      ... 54 common frames omitted
  </code></pre>
  
  <p>Not sure why spring boot throws error
  <br/>
  <strong>NOTE:spring-boot version 1.2.1</strong></p>