2013-09-16 60 views
0

我正在使用Spring批處理應用程序,並希望爲stepExecutionContext使用後期綁定。我在解決我的錯誤時遇到了問題。Spring批處理後期綁定stepExecutionContext不起作用

以下是我的讀者使用後期綁定具有SQL屬性:

<bean id="itemReader_S4_JPolicy" class="com.aegonusa.etl.readers.JDBCItemReader" scope="step"> 
    <property name="jobParameters" ref="jobParameters" /> 
    <property name="dataSource" ref="readDataSource" /> 
    <property name="rowMapper"> 
    <bean class="com.aegonusa.etl.readers.ResultSetRowMapper" scope="step" /> 
    </property> 
    <property name="sql" 
     value="#{stepExecutionContext['readQuery']}"></property> 
</bean> 

我設置在下面的類此屬性( 'readQuery'):

公共類StepListener實現StepExecutionListener {

.... ....

@Override 
public void beforeStep(StepExecution execution) { 
      String s = "select getdate(); 
    StepSynchronizationManager.getContext().setAttribute("readQuery", s); 

..... } ..... }

請注意,我能夠做到用jobParameters後期綁定。 但使用stepExecutionContext,我得到以下exc。我試過很多論壇上的帖子,但我無法來解決它:java.lang.IllegalStateException:

Throwable occurred: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lazyBindingProxy.itemReader_S4_JPolicy#sysinit' defined in class path resource [Load_InforceToStage.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot bind to placeholder: stepExecutionContext['readQuery'] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 
at java.security.AccessController.doPrivileged(AccessController.java:224) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) 
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:302) 
at org.springframework.batch.core.scope.StepScope.get(StepScope.java:150) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) 
at org.springframework.batch.core.scope.util.PlaceholderTargetSource.getTarget(PlaceholderTargetSource.java:185) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:184) 
at $Proxy1.close(Unknown Source) 
at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:83) 
at org.springframework.batch.core.step.item.ChunkMonitor.close(ChunkMonitor.java:99) 
at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:83) 
at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:297) 
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:255) 
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:135) 
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:61) 
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60) 
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144) 
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124) 
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135) 
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:281) 
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120) 
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49) 
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:114) 
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:349) 
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574) 
at com.ebsadm.lh.LHSHEDToStage.runJob(LHSHEDToStage.java:124) 
at com.ebsadm.lh.LHSHEDToStage.main(LHSHEDToStage.java:111) 
at com.ebdadm.lh.TestLHSHEDToStage.testInforceLoadToStage(TestLHSHEDToStage.java:28) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) 
at java.lang.reflect.Method.invoke(Method.java:600) 
at junit.framework.TestCase.runTest(TestCase.java:168) 
at junit.framework.TestCase.runBare(TestCase.java:134) 
at junit.framework.TestResult$1.protect(TestResult.java:110) 
at junit.framework.TestResult.runProtected(TestResult.java:128) 
at junit.framework.TestResult.run(TestResult.java:113) 
at junit.framework.TestCase.run(TestCase.java:124) 
at junit.framework.TestSuite.runTest(TestSuite.java:232) 
at junit.framework.TestSuite.run(TestSuite.java:227) 
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

所致的org.springframework.batch.core stepExecutionContext [「readQuery」] :無法綁定到佔位符.scope.util.PlaceholderTargetSource $ 1.convertIfNecessary(PlaceholderTargetSource.java:140) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1294) 在org.springframework.beans.factory.support。 AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFa ctory.populateBean(AbstractAutowireCapableBeanFactory.java:1010) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472) ...... 50多個

請幫助。

+0

您的'ResultSetRowMapper'已經是跨步範圍,因爲它綁定到封閉bean的生命週期(它已經步進了範圍)。 –

回答

1

我期待撥打getExecutionContextStepExecution給你適當的上下文。

@Override 
public void beforeStep(StepExecution execution) { 
    String s = "select getdate()"; 
    execution.getExecutionContext().putString("readQuery", s); 
} 

您目前的解決方案是獲取StepContextStepContext是公開的對象,用於表達式解析,而與StepExecution關聯的ExecutionContext是實際的stepExecutionContext。 ItemReadersItemWriters,此類對象也使用ExecutionContext來更新狀態(跳過的項目,提交計數等)。

+0

非常感謝。它像一個魅力。但是,我無法理解從execution.getExecutionContext()獲取的上下文與從StepSynchronizationManager.getContext()獲取的上下文之間的區別。他們有什麼不同? –

0

嘗試以下的任何:

  1. 使用promotionListener豆從步工作推進變量的作用域。
  2. 使用bean而不是executionContext。
相關問題