How to access jobParameters in SpringBatch as a bean?

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

I have created jobParameter bean definition like below :

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

and I am using the bean while defining reader like:

<bean id="fileDownloadReader" class="com.test.FileDownloadReader"  scope="step" >     <property name="execCtx" ref="executionContext" /> </bean> 

While invoking the job from command line am getting below exception.

with scope=step (in bean definition):

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! 

Without scope = step:

    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 

Any idea how can I get rid of this exception.

回答1:

The error indicates something different.

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] 

I think the problem is somewhere else and not by accessing to the job parameters.

P.S: Can you show the configuration where you define TestBean and set it's property reconBean?



回答2:

I've seen this before - there is something creating an AOP proxy that the classloader is using instead of the concrete implementation of your class. I've solved this in the past by introducing an interface, then calling your getBean as follows:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!