There are a few fundamental things incorrect in the code snippet.
First off jobParameters are set prior to launching your job. When creating proxies spring will not have access to those values when creating them and thus will set them to null.
Same with stepExecution. It exists only within the scope of a step inside a job.
However if you do insist on using them as constructor parameters you will have to set them explicitly later after the job has started.
One way of doing that is by using @BeforeStep annotated method in your reader
@BeforeStep
public void beforeStep(StepExecution stepExecution){
this.stepExecution = stepExecution;
}
This method runs once prior to step execution and will initialize your stepExecution with an actual valid non null instance with current step and job specific information
public class MyItemStreamReader1 implements ItemReader {
private StepExecution stepExecution;
@Value("#{jobParameters['TEST_CONFIG_1']}")
String baseConfig;
public MyItemStreamReader1( String baseConfig) {
this.baseConfig = baseConfig;
}
@BeforeStep
public void beforeStep(StepExecution stepExecution){
this.stepExecution = stepExecution;
count=0;
}
private int count;
@Override
public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
if(count==0) {
System.out.println("In "+stepExecution.getStepName()+" ***** reader instance hashCode ** "+this.hashCode() +" ***" +baseConfig);
count++;
return "";
}
return null;
}
}
This is how your reader should look like. Your second reader will be the same with the only difference being @Value("#{jobParameters['TEST_CONFIG_2']}")
String baseConfig;
This is what the config looks like
@Autowired
MyItemStreamReader1 reader1;
@Autowired
MyItemStreamReader2 reader2;
@Bean
@StepScope
public MyItemStreamReader1 itemReaderStreamConfig1(@Value("#{jobParameters['TEST_CONFIG_1']}") String baseConfig){
return new MyItemStreamReader1(baseConfig);
}
@Bean
@StepScope
public MyItemStreamReader2 itemReaderStreamConfigTwo(@Value("#{jobParameters[TEST_CONFIG_2]}") String baseConfig){
return new MyItemStreamReader2(baseConfig);
}
But let me warn you though.It makes no sense to use @Value("#{jobParameters[TEST_CONFIG_2]} in your config. Why would you want to instantiate the bean with a null value . Any job parameters are accessible only after the job starts and are set by @Before step method.
Spring will not have that information before hand and will create a reader instance with null baseConfig and stepExecution. After the job starts the annotation on baseConfig in the reader class and @BeforeStep method set the actual values.