问题
I have to autowire an object to my writer class of spring batch job. This has to be specific for job ( job scope ) I am trying to configure it my spring batch job java configuration calss and use it by autowire fashion from my writer class. But this seems to be not working and I always gets this bean as null from my writer class. What am I doing wrong.
My Job configuration class snippet :
@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();
}
And I am trying to get the bean notificationBean by autowiring from my writer class as below.
public class RdsWriter extends AbstractItemStreamItemWriter<ProducerMessage> {
@Autowired
@Qualifier("rdsJdbcTemplate")
JdbcTemplate rdsJdbcTemplate;
@Autowired
ErrorNotificationBean notificationBean ;
ErrorNotification is plain java object which has some fields.
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;
}
}
来源:https://stackoverflow.com/questions/36300601/spring-batch-autowire-bean-in-writer-not-working-by-configuring-it-via-job-java