spring-guides / gs-batch-processing

Creating a Batch Service :: Learn how to create a basic batch-driven solution.

Home Page:https://spring.io/guides/gs/batch-processing/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

no scope "step" available

waffel opened this issue · comments

Hi,

I tried to little bit enhance your nice example with the possibility to set the CSV file location from outside (instead of using the classpath).

If I modify the code from the ItemReader to

@Bean
  @Scope("step")
  public ItemReader<Person> reader() {

and then running the commandline (without any further changes!) I get following error:

    ... 37 more
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:338)
        ... 43 more

From the Spring documentation, the scope should be available (because of the Batch annotation).

Do you have any hint/idea whats the problem?

@mminella might be able to comment on this.

Hmmm…I can recreate it locally. I'll look into it further today but in the mean time, using the batch shortcut annotation @StepScope works fine.

Step scope is not available until a step is executing. Without looking at the code if know that the stack trace originated from outside a step execution. So fix that.

@dsyer When I use the completed version of this guide, adding @Scope("step") to the reader's configuration causes the exception. Using @StepScope seems to work. The full stack trace makes it look like it's trying to create the bean on the refresh.

Update: The reason this is failing is because when a bean is defined with the step scope and it's a dependency, it needs to be proxied. By default @Scope("step") will not proxy the bean. You can either use @StepScope which specifies ScopedProxymode.TARGET_CLASS or you can specify the proxy mode yourself like this: @Scope(value="step", proxyMode = ScopedProxyMode.TARGET_CLASS).

Based on @mminella 's comments, I'm closing this issue as "works-as-designed". There are no plans to make the guide as complex to involve proxying.

I had the same issue today (spring-batch 3.0.7.RELEASE) and this solution helped me to fix it.