问题描述
我想知道如何将参数传递给bean.xml。
I wonder how to pass parameter to bean.xml.
如果我在bean.xml中这样写,它会按预期工作
If I write like this in bean.xml, it works as expected
<bean id="notificationReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS ='A'" />
<property name="rowMapper">
<bean name = "campaignMapper" class="rh.com.app.domain.AgingMapper">
</bean>
</property>
</bean>
但如果我这样写,我会收到错误
But if I write like this, I get error
<property name="sql" value="SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS = #{jobParameters['edcbatchStatus']}" />
我的bean.xml
My bean.xml
<task:scheduled-tasks>
<task:scheduled ref="agingScheduler" method="run" cron="*/5 * * * * *" /><!--0 0 5 * * *-->
</task:scheduled-tasks>
<!-- class = bean-->
<bean id="agingScheduler" class="rh.com.ap.AgingScheduler">
<property name="jobLauncher" ref="jobLauncher" />
<property name="agingJob" ref="agingJob" />
<property name="mailClient" ref="mailClient" />
</bean>
<batch:job id="agingJob">
<batch:step id="step1" next = "emailFile" >
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="notificationReader" writer="notificationWriter" processor="notificationProcessor" commit-interval="10" />
</batch:tasklet>
</batch:step>
<batch:step id="emailFile">
<batch:tasklet ref="emailTasklet" />
</batch:step>
<batch:listeners>
<batch:listener ref="jobListener" />
</batch:listeners>
</batch:job>
AgingScheduler
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
builder.addString("fileName", "AgingReporting_" + PropertiseUtil.settlementDateyyyyMMdd());
builder.addString("edcbatchStatus","A").toJobParameters();
错误
Job failed with following exceptions
exception :Failed to initialize the reader
推荐答案
Hello John,
Hello John,
您可以创建一个JdbcCursorItemReader的子类,在其中设置#{jobParameters ['edcbatchStatus ']}
作为单独的参数。然后使用Springs InitializingBean将
设置为Sql属性。这样的事情怎么样?
You could create a subclass of JdbcCursorItemReader where you set #{jobParameters['edcbatchStatus']}as a seperate parameter. And then use Springs InitializingBean to setthe Sql property. How about something like this?
class EdcBatchStatusItemReader extends org.springframework.batch.item.database.JdbcCursorItemReader implements
org.springframework.beans.factory.InitializingBean {
protected String batchStatus;
public void getBatchStatus(String batchStatus) {
this.batchStatus = batchStatus;
}
public void afterPropertiesSet() {
setSql("SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS ='" + batchStatus + "'");
}
}
然后离开sql bean定义并使用setBatchStatus:
and then leave sql out of the bean definition and use setBatchStatus instead:
<bean id="notificationReader" class="EdcBatchItemReader" scope="step">
<property name="dataSource" ref="dataSource" />
<property name="batchStatus" value="#{jobParameters['edcbatchStatus']}" />
<property name="rowMapper">
<bean name = "campaignMapper" class="rh.com.app.domain.AgingMapper">
</bean>
</property>
</bean>
祝福
Marc
Best wishesMarc
这篇关于传递Spring数据中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!