通过基于作业java的配置配置它,编写器中的Spring批量autowire bean无法正常工作

时间:2016-03-30 05:44:15

标签: java spring spring-batch

我必须将一个对象自动装入我的Spring批处理作业类。这必须特定于作业(作业范围)我正在尝试将其配置为我的Spring批处理作业java配置calss并使用它来自我的编写器类的autowire时尚。但这似乎无法正常工作,我总是将这个bean从我的编写器类中取为null。我做错了什么。

我的作业配置类代码段:

@org.springframework.context.annotation.Configuration
@EnableBatchProcessing
@Component
public class ProducerBatchJobConfigurer {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StepBuilderFactory stepBuilders;

    @Bean
    @Scope("job")
    ErrorNotificationBean notificationBean(@Value("#{jobParameters[s3FilePath]}") String s3FileName,@Value("#{jobParameters[jobId]}") String jobId){
        return new ErrorNotificationBean(jobId,s3FileName);
    }

    @Bean
    @Scope("job")
    public ZipMultiResourceItemReader reader(@Value("#{jobParameters[fileName]}") String fileName, @Value("#{jobParameters[s3SourceFolderPrefix]}") String s3SourceFolderPrefix, @Value("#{jobParameters[timeStamp]}") long timeStamp, com.fastretailing.catalogPlatformSCMProducer.service.ConfigurationService confService,ErrorNotificationBean notificationBean) {
        CustomFlatFileItemReader faltFileReader = new CustomFlatFileItemReader();
        ZipMultiResourceItemReader zipReader = new ZipMultiResourceItemReader();
        Resource[] resArray = new Resource[1];
        System.out.println(notificationBean.getS3FileLocation());
        resArray[0] = new FileSystemResource(new File(fileName));
        zipReader.setArchives(resArray);
        DefaultLineMapper<ProducerMessage> lineMapper = new DefaultLineMapper<ProducerMessage>();
        lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
        CSVFieldMapper csvFieldMapper = new CSVFieldMapper(fileName, s3SourceFolderPrefix, timeStamp, confService);
        lineMapper.setFieldSetMapper(csvFieldMapper);
        faltFileReader.setLineMapper(lineMapper);
        zipReader.setDelegate(faltFileReader);
        return zipReader;
    }

    @Bean
    @ConfigurationProperties
    @Scope("job")
    public ItemStreamWriter<ProducerMessage> writer(ErrorNotificationBean notificationBean) {
        return new RdsWriter();
    }

我正试图通过我的编写器类自动装配来获取bean notificationBean,如下所示。

public class RdsWriter extends AbstractItemStreamItemWriter<ProducerMessage> {

    @Autowired
    @Qualifier("rdsJdbcTemplate")
    JdbcTemplate rdsJdbcTemplate;

    @Autowired
    ErrorNotificationBean notificationBean ;

ErrorNotification是普通的java对象,它有一些字段。

package com.fastretailing.catalogPlatformSCMProducer.model;

import org.springframework.stereotype.Component;

/**
 * Bean for Error Notifications.
 */
@Component
public class ErrorNotificationBean {
    private String subject ;
    private int rowsSkipped;
    private String feedName;
    private String s3FileLocation;
    private String uniqueId;

    public ErrorNotificationBean(String uniqueId, String s3FileLocation) {
        this.uniqueId = uniqueId;
        this.s3FileLocation = s3FileLocation;
    }
    public ErrorNotificationBean(){

    }
    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public int getRowsSkipped() {
        return rowsSkipped;
    }

    public void setRowsSkipped(int rowsSkipped) {
        this.rowsSkipped = rowsSkipped;
    }

    public String getFeedName() {
        return feedName;
    }

    public void setFeedName(String feedName) {
        this.feedName = feedName;
    }

    public String getUniqueId() {
        return uniqueId;
    }

    public void setUniqueId(String uniqueId) {
        this.uniqueId = uniqueId;
    }

    public String getS3FileLocation() {
        return s3FileLocation;
    }

    public void setS3FileLocation(String s3FileLocation) {
        this.s3FileLocation = s3FileLocation;
    }
}

0 个答案:

没有答案