Spring批处理 - 使用Java Bean时的循环引用错误,但应用程序通过使用xml bean正常运行

时间:2015-02-12 12:14:35

标签: spring spring-batch

Bean“delegateItemWriter”和“itemWriter”互相循环调用。 当我评论xml bean(运行正常)并使用java bean运行时,它给出了错误,因为“请求的bean当前正在创建”。 任何人都可以帮助为什么这只发生在java bean而不是xml bean? 任何帮助将不胜感激。

批处理作业定义:

<job id="fileToFileWithHeaderFooterJob" job-repository="jobRepository">
    <step id="step">
        <tasklet>
            <chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
                commit-interval="2">
                <streams>
                    <stream ref="delegateItemWriter" />
                </streams>
            </chunk>
            <listeners>
                <listener ref="itemWriter" />
            </listeners>
        </tasklet>
    </step>
</job>

如果我在xml bean(delegateItemWriter)下面发表评论,那么我的应用程序会中断并给出错误创建名为'delegateItemWriter'的bean时出错:请求bean当前正在创建:是否存在无法解析的循环引用?

<beans:bean id="delegateItemWriter"
    class="org.springframework.batch.item.file.FlatFileItemWriter">
    <beans:property name="resource" ref="outputFileResource" />
    <beans:property name="shouldDeleteIfExists" value="true" />
    <beans:property name="lineAggregator">
        <beans:bean
            class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <beans:property name="delimiter" value="," />
            <beans:property name="fieldExtractor">
                <beans:bean
                    class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <beans:property name="names"
                        value="newId,newName,newDate,newParty,newPrice" />
                </beans:bean>
            </beans:property>
        </beans:bean>
    </beans:property>
    <beans:property name="headerCallback" ref="headerCallback" />
    <beans:property name="footerCallback" ref="itemWriter" />
</beans:bean>


Java Config:

@Bean
public FlatFileItemReader<Object> itemReader() {
    FlatFileItemReader<Object> itemReader = new FlatFileItemReader<>();
    DefaultLineMapper<Object> lineMapper = new DefaultLineMapper<>();
    DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();

    lineTokenizer.setNames(new String[] {"dealId", "price", "name", "party", "Date"});

    lineMapper.setLineTokenizer(lineTokenizer);
    lineMapper.setFieldSetMapper(new TradeDataFieldSetMapper());

    itemReader.setResource(new ClassPathResource("inputData.txt"));
    itemReader.setLineMapper(lineMapper);
    return itemReader;
}

@Bean
public SimpleItemProcessor itemProcessor() {
    return new SimpleItemProcessor();
}

@Bean
    public FileToFileFooterCallback itemWriter(final ItemWriter<TradeDataOutput> delegetItemWriter) {
    FileToFileFooterCallback footerCallback = new FileToFileFooterCallback();
    footerCallback.setDelegate(delegetItemWriter);
    return footerCallback;
}


@Bean
public FlatFileItemWriter<TradeDataOutput> delegateItemWriter(final FileToFileFooterCallback itemWriter) {
    FlatFileItemWriter<TradeDataOutput> writer = new FlatFileItemWriter<>();
    LineAggregator<TradeDataOutput> delimitedLineAggregator = new DelimitedLineAggregator<>();
    FieldExtractor<TradeDataOutput> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();

    String[] names = {"newID", "newName", "newDate", "newParty", "newPrice"};
    ((BeanWrapperFieldExtractor<TradeDataOutput>) beanWrapperFieldExtractor).setNames(names);

    ((DelimitedLineAggregator<TradeDataOutput>) delimitedLineAggregator).setDelimiter(",");
    ((DelimitedLineAggregator<TradeDataOutput>) delimitedLineAggregator).setFieldExtractor(beanWrapperFieldExtractor);

    writer.setLineAggregator(delimitedLineAggregator);
    writer.setResource(new FileSystemResource(
        "batch/filetoFileWithHeaderFooterOutputFile.data"));
    writer.setShouldDeleteIfExists(true);
    writer.setHeaderCallback(new FileToFileHeaderCallback());
    writer.setFooterCallback((FlatFileHeaderCallback) itemWriter);
    return writer;
}

0 个答案:

没有答案