java 在 Spring Batch 中将 jobParameters 传递给 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17549265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:17:26  来源:igfitidea点击:

Passing jobParameters to bean in Spring Batch

javaspringspring-batch

提问by Sach

I read a problem from the link https://stackoverflow.com/q/15784984/814074and tried the solution given in above link. However, I got the following error while running the code:

我从链接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

The xml contains

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>

Am I missing anything?

我错过了什么吗?

回答by Cygnusx1

The easy way to use step scope is like this:

使用步骤范围的简单方法是这样的:

<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>

Placing the step scope on the beans will delay his creation until the step it his referred is about to start. This is what Late-binding means, so you could access variables in the the ExecutionContext.

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

As the docs in the StepScope states:

正如 StepScope 中的文档所述:

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

带有 StepScope 的 bean 将是 aop:scoped-proxy。这意味着代理会绕过真实对象。

So when you define a regular Spring Beans (like you did with jobArg) and put the scope=step on it. You will have to find a way to retrieve the object inside this proxy when you want to set it in another bean (JobArgs)

所以当你定义一个普通的 Spring Beans(就像你对 jobArg 所做的那样)并把 scope=step 放在它上面。当您想在另一个 bean (JobArgs) 中设置它时,您必须找到一种方法来检索此代理中的对象

回答by Mihai Soloi

I have looked all over the Spring references and I have not seen late binding in the constructor arguments so I am not sure it works. I assume you are setting the jobParameters in the JobArgs, in which case it should have the same scope="step"

我查看了所有 Spring 引用,但没有看到构造函数参数中的后期绑定,所以我不确定它是否有效。我假设您在 中设置 jobParameters JobArgs,在这种情况下它应该具有相同的scope="step"