我正在嘗試將作業參數注入到自定義的ItemReader中。我已經回顧了關於這個主題的所有StackOverflow筆記(例如:How to get access to job parameters from ItemReader, in Spring Batch?),並且我看到這是一個常見的痛點,大部分都是未解決的。我希望春天的大師(@Michael Minella任何人)都會看到這個並有一些洞察力。來自Spring批處理作業參數
儘管沒有代碼或配置更改,但我仍然確定作業參數可用於大約10次運行中的一次。這是一個隨機成功的案例,而不是隨機的失敗,所以很難追查到。
我用調試器挖入了Spring代碼,並確定當這個失敗時,名爲jobParameters的Bean在注入發生時在Spring中註冊。
我使用Spring 4.1.4彈簧批次3.0.2和彈簧數據的JPA 1.7.1和彈簧數據公地1.9.1,在Java運行8
的Java
類@Component("sourceSelectionReader")
@Scope("step")
public class SourceSelectionReaderImpl
implements ItemReader<MyThing> {
private Map<String,Object> jobParameters;
// ... snip ...
@Autowired
@Lazy
@Qualifier(value="#{jobParameters}")
public void setJobParameters(Map<String, Object> jobParameters) {
this.jobParameters = jobParameters;
}
}
工作啓動參數:
launch-context.xml job1 jobid(long)=1
推出-context.xml中(減去絨毛):
<context:property-placeholder location="classpath:batch.properties" />
<context:component-scan base-package="com.maxis.maximo.ilm" />
<jdbc:initialize-database data-source="myDataSource" enabled="false">
<jdbc:script location="${batch.schema.script}" />
</jdbc:initialize-database>
<batch:job-repository id="jobRepository"
data-source="myDataSource"
transaction-manager="transactionManager"
isolation-level-for-create="DEFAULT"
max-varchar-length="1000"/>
<import resource="classpath:/META-INF/spring/module-context.xml" />
模塊-context.xml中(減去絨毛):
<description>Example job to get you started. It provides a skeleton for a typical batch application.</description>
<import resource="classpath:/META-INF/spring/hibernate-context.xml"/>
<import resource="classpath:/META-INF/spring/myapp-context.xml"/>
<context:component-scan base-package="com.me" />
<bean class="org.springframework.batch.core.scope.StepScope" />
<batch:job id="job1">
<batch:step id="step0002" >
<batch:tasklet transaction-manager="transactionManager" start-limit="100" >
<batch:chunk reader="sourceSelectionReader" writer="selectedDataWriter" commit-interval="1" />
</batch:tasklet>
</batch:step>
</batch:job>
你如何啓動這項工作? – wassgren
而且,你爲什麼使用'@ Lazy'進行注射?這是你的任務需要嗎? – wassgren
現在,我正在從我的eclipse IDE中啓動該作業。它將在命令行啓動時啓動。 我使用@Lazy,因爲它允許我避開這個問題,並繼續進行真正的項目,等待解決。還有其他方法可以將這些參數導入到bean中,但是從長遠來看,它們會產生支持問題。 –