【问题标题】:How to input parameters in a job如何在作业中输入参数
【发布时间】:2019-02-11 09:50:23
【问题描述】:

我正在尝试通过控制台输入特定参数以运行作业,如下所示:

@Value("#{jobParameters['inputFileName']}")
public void setFileName(final String name) {
    inputFileName = name;
}

输入参数是定义为“ficheroEntrada.csv”的 inputFileName,如下所示: https://imgur.com/m2jVoEB

但我收到此错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchConfiguration': Unsatisfied dependency expressed through method 'setFileName' parameter 0; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:666) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
                at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
                at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
                at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
                at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
                at com.santalucia.batch.md_mig.Application.main(Application.java:25) [classes/:na]
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
                at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1452) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1085) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:658) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                ... 18 common frames omitted
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
                at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:52) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:111) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:265) ~[spring-expression-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
                ... 22 common frames omitted

我是 spring-batch 世界的新手,我真的不知道我做错了什么......

提前感谢您的帮助。

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

    要绑定作业参数,您需要将late binding 功能与StepScope 结合使用。

    您需要做的是使您的 setter 在其中定义为步进范围的 bean。在您的情况下,一种典型的方法是将项目阅读器声明为 bean 并按如下方式传递作业参数:

    @Bean
    @StepScope
    public FlatFileItemReader flatFileItemReader(@Value("#{jobParameters['inputFileName']}") String name) {
       return new FlatFileItemReaderBuilder<Foo>()
                   .name("flatFileItemReader")
                   .resource(new FileSystemResource(name))
                   // set other properties on the reader
    }
    

    【讨论】:

    • 也就是说,Spring 可以为您进行类型转换,在这种情况下,您也可以注入 Resource 而不是 String。
    【解决方案2】:

    使用 Spring,您应该能够通过使用 @Value 注释和 ${} 语法来保留命令行参数。

    public void setFileName(@Value("${inputFileName}") final String name) {
        inputFileName = name;
    }
    

    显然@Value 注释适用于由 Spring 上下文 (Bean) 管理(有时是代理)的对象。

    【讨论】:

    • 即使我删除了“jobParameters”语法,我也会遇到同样的错误......
    • @Albertus_Farray 你不必这样做。它应该已经由 Spring 管理。可以贴一下课程代码吗?
    • 是的,我可以发布它,让我等一下
    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多