猿问

如何在作业中输入参数

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


@Value("#{jobParameters['inputFileName']}")

public void setFileName(final String name) {

    inputFileName = name;

}

输入参数是 inputFileName,定义为“ficheroEntrada.csv”,如下所示: 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]

我是春季批次世界的新手,我真的不知道我做错了什么......


提前感谢您的帮助。


汪汪一只猫
浏览 93回答 2
2回答

泛舟湖上清波郎朗

要绑定作业参数,您需要将后期绑定功能与StepScope.您需要做的是将您的 setter 定义为步进范围的 bean。在您的情况下,一种典型的方法是将项目阅读器声明为 bean 并按如下方式传递作业参数:@Bean@StepScopepublic FlatFileItemReader flatFileItemReader(@Value("#{jobParameters['inputFileName']}") String name) {&nbsp; &nbsp;return new FlatFileItemReaderBuilder<Foo>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.name("flatFileItemReader")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.resource(new FileSystemResource(name))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// set other properties on the reader}

ibeautiful

使用 Spring,您应该能够通过使用带有语法的@Value注释来获取命令行参数。${}public void setFileName(@Value("${inputFileName}") final String name) {&nbsp; &nbsp; inputFileName = name;}显然,@Value注释适用于由 Spring 上下文(Beans)管理(有时是代理)的对象。
随时随地看视频慕课网APP

相关分类

Java
我要回答