我试图将作业参数注入自定义ItemReader.我已经回顾了有关该主题的所有StackOverflow说明(例如:
How to get access to job parameters from ItemReader, in Spring Batch?),我发现这是一个常见的难点,大部分都没有解决.我希望春天的大师(@Michael Minella任何人)会看到这一点,并有一些见解.
即使没有代码或配置更改,我已经确定了大约十分之一的运行参数可用.这是随机成功而非随机失败的情况,因此很难追查.
我使用调试器挖掘了spring代码,并确定当失败时,在注入发生时没有在Spring中注册名为jobParameters的bean.
我使用Spring 4.1.4和spring-batch 3.0.2以及spring-data-jpa 1.7.1和spring-data-commons 1.9.1,在java 8中运行.
Java类
@Component("sourceSelectionReader")
@Scope("step")
public class SourceSelectionReaderImpl
implements ItemReader {
private Map jobParameters;
// ... snip ...
@Autowired
@Lazy
@Qualifier(value="#{jobParameters}")
public void setJobParameters(Map jobParameters) {
this.jobParameters = jobParameters;
}
}
工作启动参数:
launch-context.xml job1 jobid(long)=1
launch-context.xml(减去绒毛):
data-source="myDataSource"
transaction-manager="transactionManager"
isolation-level-for-create="DEFAULT"
max-varchar-length="1000"/>
Module-context.xml(减去绒毛):
Example job to get you started. It provides a skeleton for a typical batch application.