java - Spring 批处理 : scope ("step") failed

标签 java spring

我可以在 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

关于java - Spring 批处理 : scope ("step") failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467589/

相关文章:

javax.servlet.ServletException : java. lang.NoClassDefFoundError:JspException

java - 处理程序滞后于应用程序

java - 建议在单个 IntelliJ 项目中使用 2 个 Web 模块吗?

spring - 配置 Spring 以忽略使用 @Inject 注释的依赖项

java - JPA : EntityManager is taking too long to save the data

java - 静态方法、新线程性能问题

java - Eclipse IDE - 悬停错误后信息气球不起作用

java - 通过 Websphere over SSL 连接到 Oracle 的 RSA premaster secret 错误

java - 连接两个表并在 thymeleaf 中显示检索到的信息

java - 向 MQ 发送消息时如何删除默认的 Spring JMS 模板 header ?