繁体   English   中英

弹簧批次:范围(“步骤”)失败

[英]spring batch: scope(“step”) failed

我可以在xml配置中使用scope =“step”而没有任何问题,但是如果将它用作注释,如下所示。 它会引发以下错误

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

重点是:

范围'step'对当前线程无效;

根本原因是什么?

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;
    }

您需要使用以下两种方式之一

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

要么

 @Bean
 @StepScope

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM