Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring batch: scope("step") failed

Tags:

java

spring

I could use scope="step" in xml configuration without any issue, but if use it as annotation as below . it throws the following error

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'step1' defined in class path resource [BatchConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.batch.item.ItemReader]: : Error creating bean with name 'reader': 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; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reader': 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

The main point is:

Scope 'step' is not active for the current thread;

what's the root cause?

public class BatchConfiguration {

    @Bean
    @Scope("step")
    public ItemReader<Source> reader(@Value("#{jobParameters['fileName']}") String fileName) {
        System.out.println("*****************************");
        System.out.println("read file of "+fileName);
        System.out.println("*****************************");
        FlatFileItemReader<Source> reader = new FlatFileItemReader<Source>();

        reader.setResource(new FileSystemResource(fileName));
        reader.setLineMapper(new DefaultLineMapper<Source>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] { "firstName", "lastName" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Source>() {{
                setTargetType(Source.class);
            }});
        }});
        return reader;
    }
like image 631
Daniel Wu Avatar asked Dec 14 '14 08:12

Daniel Wu


1 Answers

You need to use either of below to two ways available

   @Scope(value="step",
   proxyMode=TARGET_CLASS)

Or

 @Bean
 @StepScope
like image 158
Panther Avatar answered Sep 28 '22 05:09

Panther