Spring batch 如何作为bean访问SpringBatch中的jobParameters?

Spring batch 如何作为bean访问SpringBatch中的jobParameters?,spring-batch,Spring Batch,我创建了jobParameter bean定义,如下所示: <bean id="executionContext" class="com.test.ExecutionContextImpl" scope="step" > <property name="toDate" value="#{jobParameters['toDate']}" /> <property name="fromDate" value="#{jobParameters['fro

我创建了jobParameter bean定义,如下所示:

<bean id="executionContext" class="com.test.ExecutionContextImpl" scope="step" >
    <property name="toDate" value="#{jobParameters['toDate']}"  />
    <property name="fromDate" value="#{jobParameters['fromDate']}"  />
</bean>
无范围=步骤:

2013/07/01 08-52-19,057:OUT:ERROR[Encountered an error executing the step]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fileDownloadReader' defined in class path resource [launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.test.components.TestBean' for property 'reconBean'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.TestBean] for property 'reconBean': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:333)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:150)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:329)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:182)
    at $Proxy17.beforeStep(Unknown Source)
    at org.springframework.batch.core.listener.CompositeStepExecutionListener.beforeStep(CompositeStepExecutionListener.java:76)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:191)
    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:293)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    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)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.test.components.TestBean' for property 'reconBean'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.TestBean] for property 'reconBean': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:465)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:496)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1437)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    ... 23 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.ShipmentReconBean] for property 'reconBean': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:450)
    ... 29 more
2013/07/01 08-52-19,057:ERR:ERROR[Encountered an error executing the step]

[org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean threw org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fileDownloadReader' defined in class path resource [launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.test.components.TestBean' for property 'reconBean'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.TestBean] for property 'reconBean': no matching editors or conversion strategy found]
2013/07/01 08-52-19,098:OUT:ERROR[Exception in afterStep callback]

[org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean threw org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fileDownloadReader' defined in class path resource [launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.test.components.TestBean' for property 'reconBean'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.TestBean] for property 'reconBean': no matching editors or conversion strategy found]
2013/07/01 08-52-19,098:ERR:ERROR[Exception in afterStep callback]

[org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean threw org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fileDownloadReader' defined in class path resource [launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.test.components.TestBean' for property 'reconBean'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.TestBean] for property 'reconBean': no matching editors or conversion strategy found]
Batch Execution Failed!
    2013/06/30 14-17-32,299:OUT:ERROR[Job Terminated in error: Error creating bean with name 'fileDownloadReader' defined in class path resource [launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext']
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileDownloadReader' defined in class path resource [launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:282)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574)
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142)
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1300)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:211)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:183)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    ... 12 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:107)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:48)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:81)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:89)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139)
    ... 18 more
2013/06/30 14-17-32299:OUT:错误[作业因错误而终止:创建名为“fileDownloadReader”的bean时出错,该bean在类路径资源[launch context.xml]中定义]:bean初始化失败;嵌套异常为org.springframework.beans.factory.BeanExpressionException:表达式解析失败;嵌套异常为org.springframework.Expression.spel.SpelEvaluationException:EL1008E:(位置0):在“org.springframework.beans.factory.config.BeanExpressionContext”类型的对象上找不到字段或属性“jobParameters”]
org.springframework.beans.factory.BeanCreationException:创建名为“fileDownloadReader”(在类路径资源[launch context.xml]中定义)的bean时出错:bean初始化失败;嵌套异常为org.springframework.beans.factory.BeanExpressionException:表达式解析失败;嵌套异常为org.springframework.expression.spel.SpelEvaluationException:EL1008E:(位置0):在类型为“org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到字段或属性“jobParameters”
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:607)
位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)
位于org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:282)
位于org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574)
原因:org.springframework.beans.factory.BeanExpressionException:表达式解析失败;嵌套异常为org.springframework.expression.spel.SpelEvaluationException:EL1008E:(位置0):在类型为“org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到字段或属性“jobParameters”
位于org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142)
位于org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1300)
位于org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:211)
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.ResolveValueIfNeeded上(BeanDefinitionValueResolver.java:183)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
... 还有12个
原因:org.springframework.expression.spel.SpelEvaluationException:EL1008E:(位置0):在类型为“org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到字段或属性“jobParameters”
位于org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)
位于org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
位于org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:107)
位于org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:48)
位于org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:81)
位于org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
位于org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:89)
位于org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139)
... 还有18个

你知道我怎样才能摆脱这个异常吗。

这个错误表明了一些不同的东西

Cannot convert value of type [$Proxy13 implementing java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.test.components.TestBean] for property 'reconBean': no matching editors or conversion strategy found]
我认为问题出在其他地方,而不是通过访问作业参数


附言:您能展示一下您定义TestBean并设置其属性reconBean的配置吗?

我以前见过这一点-有一些东西正在创建一个AOP代理,类加载器正在使用它,而不是类的具体实现。我在过去通过引入一个接口解决了这个问题,然后调用您的getBean,如下所示:

FileDownloadReadable fileDownloadReader = applicationContext.getBean("fileDownloadReader", FileDownloadReadable.class);

你已经看到了吗?这对你的问题有帮助吗?我已经试过了。但无法找出根本原因。例外情况是,它试图将一个对象投射到您的计算机中
FileDownloadReadable fileDownloadReader = applicationContext.getBean("fileDownloadReader", FileDownloadReadable.class);