我正在尝试访问 itemReader 中的 JobParameters,我遵循了 Spring 用户指南中的“作业和步骤属性的后期绑定(bind)”,但无法使其工作。
这是我的配置: Spring 批处理 2.1.8 Spring-core 3.0.5
<bean id="ipcFileReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="#{jobParameters['filename']}"/>
<property name="lineMapper" ref="lineMapper"/>
<property name="comments" value="#"/>
</bean>
我的测试类:
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,StepScopeTestExecutionListener.class})
@ContextConfiguration
public class IpcFileReaderTest {
@Autowired
private ItemReader<Client> ipcFileReader;
public StepExecution getStepExecution(){
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putString("filename",System.getProperty("user.dir") + "/src/test/resources/OMC_File/OMC_Test_1.csv");
return execution;
@Test
public void testReader() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception{
Assert.assertNotNull(ipcFileReader.read());
}
}
但我总是遇到这个错误:
Error creating bean with name 'lazyBindingProxy.lazyBindingProxy.ipcFileReader#execution#1234' defined in class path resource [applicationContext-Ipc.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot bind to placeholder: jobParameters['filename']
有什么想法吗?
谢谢
最佳答案
实际上,这是由于一个非常小的错字造成的。
#{jobParameters['filename']}
应该是:
#{jobParameters[filename]}
不需要单引号。
关于java - ItemReader 的作业参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6440817/