我想知道如何将参数传递给bean.xml。
如果我在 bean.xml 中这样写,它会按预期工作
<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>
但是如果我这样写,就会出错
<property name="sql" value="SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS = #{jobParameters['edcbatchStatus']}" />
我的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>
老化调度程序
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
最佳答案
约翰你好,
您可以创建 JdbcCursorItemReader 的子类,在其中设置 #{jobParameters['edcbatchStatus']} 作为一个单独的参数。然后使用Springs InitializingBean来设置 Sql 属性。像这样的怎么样?
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 代替:
<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>
最美好的祝愿 马克
关于java - Spring数据中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42008631/