首页 > 代码库 > springbatch 给自定义的processor传递JobParameters中设置的参数

springbatch 给自定义的processor传递JobParameters中设置的参数

1.在配置文件中添加对应的属性值

<bean id="accFeeChannelProcessor" class="com.ninefbank.smallpay.admin.test.AccFeeChannelProcessor" scope="step">
        <property name="loginName" value="#{jobParameters[‘loginName‘]}" />
    </bean>

2.在逻辑类中添加对应的属性以及set方法

package com.ninefbank.smallpay.admin.test;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;

import com.ninefbank.smallpay.admin.feemgr.vo.AccFeeChannel;

/**
 * 
 * @author GMM
 *
 */
//@Service("accFeeChannelProcessor")
public class AccFeeChannelProcessor implements ItemProcessor<AccFeeChannel, AccFeeChannel> {
    private static final Logger logger=LoggerFactory.getLogger(AccFeeChannelProcessor.class);
    private String loginName ;
    public void setLoginName(String loginName) {  
        this.loginName = loginName;  
    }  
    
    @Override
    public AccFeeChannel process(AccFeeChannel paramI) throws Exception {
        logger.info("springbatch 中读出来的对象为:{}",new Object[]{paramI.toString()});
        paramI.setCreator(loginName);
        paramI.setUpdator(loginName);
        Date date = new Date();
        paramI.setCreateTime(date);
        paramI.setUpdateTime(date);
        logger.info("填充之后的对象信息为:{}",new Object[]{paramI.toString()}); 
        return paramI;
    }
    
    
}

配置文件中的 

scope="step"必须得加上 不加上是会报错的:

Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘localToDatabaseStep3‘: Cannot resolve reference to bean ‘accFeeChannelProcessor‘ while setting bean property ‘itemProcessor‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘accFeeChannelProcessor‘ defined in class path resource [job/job-fee.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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:334) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1418) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1159) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:615) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410) ~[spring-web-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068) [catalina.jar:7.0.69]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584) [catalina.jar:7.0.69]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:7.0.69]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572) [catalina.jar:7.0.69]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562) [catalina.jar:7.0.69]
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) [na:1.7.0_13]
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) [na:1.7.0_13]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_13]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_13]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_13]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘accFeeChannelProcessor‘ defined in class path resource [job/job-fee.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:529) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    ... 25 common frames omitted
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) ~[spring-context-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1315) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:214) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:186) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1418) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1159) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ~[spring-beans-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    ... 31 common frames omitted
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:216) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:85) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:78) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:49) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:82) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:89) ~[spring-expression-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139) ~[spring-context-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    ... 37 common frames omitted
[smallpay-newaccount-admin][2017-07-14 17:31:50 620] INFO  [localhost-startStop-1][org.springframework.web.context.ContextLoader:285]- Root WebApplicationContext: initialization started

百度了一下原来是springbatch的公主病在作祟:这是SpringBatch的一个后绑定技术,就是在生成Step的时候,才去创建bean,因 为这个时候jobparameter才传过来。如果加载配置信息的时候就创建bean,这个时候jobparameter的值还没有产生,会抛出异常。

springbatch 给自定义的processor传递JobParameters中设置的参数