我正在尝试创建一个 bean,但遇到了一些问题。这是我目前的配置。
@Bean(name = TDA_JDBC_CURSOR_ITEM_READER)
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public TdaJdbcCursorItemReader<PostDatedAchIn> tdaJdbcCursorItemReader(@Value("#{jobExecutionContext['current.business.date']}") final String currentBusinessDate) {
SqlStatementHelper sqlStatementHelper = new SqlStatementHelper(PostDatedAchInConstants.POST_DATED_ACH_IN_PROPERTY_FILE,
PostDatedAchInConstants.POST_DATED_ACH_IN_PROPERTY_ALT_FILE);
String sqlStatement = sqlStatementHelper.getSqlStatement(PostDatedAchInConstants.RETRIEVE_POST_DATED_ACH_IN);
TdaJdbcCursorItemReader<PostDatedAchIn> tdaJdbcCursorItemReader = new TdaJdbcCursorItemReader<PostDatedAchIn>();
tdaJdbcCursorItemReader.setDataSource(prodDataSource);
tdaJdbcCursorItemReader.setSql(sqlStatement);
tdaJdbcCursorItemReader.setRowMapper(new PostDatedAchInRowMapper());
ArrayList<String> parameters = new ArrayList<String>();
parameters.add(currentBusinessDate);
parameters.add(currentBusinessDate);
parameters.add(currentBusinessDate);
ListPreparedStatementSetter preparedStatementSetter = new ListPreparedStatementSetter();
preparedStatementSetter.setParameters(parameters);
tdaJdbcCursorItemReader.setPreparedStatementSetter(preparedStatementSetter);
return tdaJdbcCursorItemReader;
}
主要问题是没有 ScopedProxyMode,它使用接口并在稍后在作业中尝试使用 bean 时崩溃。但是当我添加范围时,它会在尝试访问作业执行上下文时爆炸。那么我怎样才能用这个 bean 实现这两件事呢?
更新我更改为 @JobScope 并且上下文没有问题,但是在尝试实例化 bean 并打开数据库时出现此异常。
2018-10-31 09:00:43,994 ERROR [o.s.batch.core.step.AbstractStep:AbstractStep.java:execute:229 - main] - Encountered an error executing step achFulfillment_postDatedAchInJob_step1_scheduleAchIns in job achFulfillmentPostDatedAchInJob
java.lang.ClassCastException: com.sun.proxy.$Proxy110 cannot be cast to org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader$$FastClassBySpringCGLIB$$ebb633d0.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.tdameritrade.commons.batch.TdaJdbcCursorItemReader$$EnhancerBySpringCGLIB$$9110d31d.open(<generated>)
at com.tdameritrade.ctg.ach.fulfillment.util.AggregatingJdbcItemReader.open(AggregatingJdbcItemReader.java:161)
at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
at com.tdameritrade.ctg.ach.fulfillment.util.MoneyMovementHelper.getJobExecution(MoneyMovementHelper.java:155)
at com.tdameritrade.ctg.ach.fulfillment.util.MoneyMovementHelper.getJobExecution(MoneyMovementHelper.java:136)
at com.tdameritrade.ctg.ach.batch.postdatedachinjob.PostDatedAchInBatchConfigTest.testLaunchJob(PostDatedAchInBatchConfigTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)