gpt4 book ai didi

java - 在 Spring Batch 中将 jobParameters 传递给 bean

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:00 26 4
gpt4 key购买 nike

我从链接 https://stackoverflow.com/q/15784984/814074 读到一个问题并尝试了上面链接中给出的解决方案。但是,我在运行代码时遇到以下错误:

Error creating bean with name 'JobArgs' defined in class path resource [pipelineJob.xml]: 
Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy2 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.genepanel.job.JobArguments' for property 'jobArguements'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [$Proxy2 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.genepanel.job.JobArguments] for property 'jobArguements': no matching editors or conversion strategy found

xml 包含

   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<batch:job id="pipelineJob">
<batch:step id="initializationStep" next="CleanUPStep">
<batch:tasklet ref="initializationStepTasklet" />
</batch:step>
<batch:step id="CleanUPStep">
<batch:tasklet ref="cleanupTaskLet" />
</batch:step>
</batch:job>



<bean id="basicStep" class="com.test.mutation.steps.BasicStep" abstract="true">
<property name="testJobArgs" ref="JobArgs"/>
</bean>

<bean id="JobArgs" class="com.test.mutation.application.TestJobArguements">
<property name="jobArguements" ref="jobArg">
</property>
</bean>

<bean id="jobArg" class="com.test.genepanel.job.JobArguments" scope="step">
<constructor-arg value="#{jobParameters['jobOutputDir']}"/>
</bean>

<bean id="emptyTaskLet" class="com.test.mutation.steps.EmptyStep" scope="step" parent="basicStep" />

<bean id="cleanupTaskLet" class="com.test.mutation.steps.CleanUpStep" scope="step" parent="basicStep">
</bean>

<bean id="initializationStepTasklet" class="com.test.mutation.steps.InitializationStep" scope ="step" parent="basicStep">
</bean>
</beans>

我错过了什么吗?

最佳答案

使用步骤范围的简单方法如下:

<bean id="myReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="file:#{jobParameters['input.file']}" />
<property name="linesToSkip" value="0" />
<property name="recordSeparatorPolicy" ref="simpleRecordPolicy" />
<property name="lineMapper" ref="journalSicIemtLineMapper" />
</bean>

将步骤范围放在 bean 上将延迟他的创建,直到他引用的步骤即将开始。这就是后期绑定(bind)的含义,因此您可以访问 ExecutionContext 中的变量。

正如 StepScope 中的文档所述:

beans with the StepScope will be aop:scoped-proxy. This means a proxy goes around the real object.

因此,当您定义常规 Spring Beans(就像您对 jobArg 所做的那样)并在其上添加scope=step 时。当您想在另一个 bean (JobArgs) 中设置对象时,您必须找到一种方法来检索该代理内的对象

关于java - 在 Spring Batch 中将 jobParameters 传递给 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17549265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com