我相信我在这里错过了一些非常基本的东西。我正在修改一个spring-batch批处理作业,以便在作业运行时输入一个参数,以允许将临时文件存储在每个作业运行的临时文件夹的时间戳中。当前,所有临时文件都位于同一个文件夹中,如果作业不按顺序运行,则这些临时文件会相互崩溃。
我有一个作业运行程序蝙蝠文件,我可以使用它来实际开始作业,它正在创建一个时间戳并将其附加到传递给CommandLineJobRunner的参数上。因此它运行带有3个参数的CommandLineJobRunner.main(args):
String[] args = new String[]{[File.XML], [JobName], build.timestamp=[timestamp]}
CommandLineJobRunner.main(args)
在我的XML中,我有以下内容
<bean id="tempfiledir" class="org.springframework.core.io.FileSystemResource">
<constructor-arg value="${batch.file.drive}:/${batch.file.writer.input.root.directory}/${batch.input_file}/${build.timestamp}" />
</bean>
那里的其他变量在我的batch.properties文件中设置。它们显然是静态的。
从那里,我像这样将资源传递到Bean中:
<bean id="[beanTaskletID]" class="[taskletDir]">
<property name="temporaryFilesDir" ref="tempfiledir"/>
</bean>
然后在实际步骤中:
<step id="[stepID]">
<tasklet ref="[beanTaskletID]"/>
<next on="FAILED" to="[failStepID]"/>
<next on="COMPLETED" to="[completeStepID]"/>
</step>
在我的小任务内,我得到了传递的参数,如下所示:
Resource tempDirectory;
public void setTemporaryFilesDir(Resource temporaryFilesDir) {
this.tempDirectory = temporaryFilesDir;
}
当我尝试运行此命令时,出现以下错误:
lassPathXmlApplicationContext [WARN] Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'tempfiledir' defined in class path resource [XML FILE]: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:155)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:590)
at com.batch.myJobRunner.main(myJobRunner.java:28)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitGenericArgumentValues(BeanDefinitionVisitor.java:159)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85)
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208)
... 11 more
2020-02-14 15:49:36 CommandLineJobRunner [ERROR] Job Terminated in error: Invalid bean definition with name 'tempfiledir' defined in class path resource [XML FILE]: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.nput_file}/${build.timestamp}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'tempfiledir' defined in class path resource [XML FILE]: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:155)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:590)
at com.batch.myJobRunner.main(myJobRunner.java:28)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'build.timestamp' in string value "${batch.file.drive}:/${batch.file.writer.input.root.directory}${batch.input_file}/${build.timestamp}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitGenericArgumentValues(BeanDefinitionVisitor.java:159)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85)
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208)
... 11 more
我想也许我需要一个声明的占位符来设置参数,所以我在batch.properties中添加了以下内容:
build.timestamp=
我已阅读到运行时传递的作业参数将覆盖同一参数的任何属性文件设置。但是,当我像这样运行作业时,build.timestamp始终只是一个空字符串。因此,要么不是这种情况,要么是在工作运行中传入的参数实际上没有在任何地方设置。
要么,要么我非常清楚地不知道如何从XML文件中运行的作业访问传入的参数。任何帮助如何做到这一点将不胜感激。我已经花了几个小时来浏览spring-batch文档,但是没有任何东西可以清楚地表明如何访问XML文件中的参数,只要您可以进行操作,便可以完成所有操作。我找到的用于访问此类参数的所有文档都可以通过在Java中创建作业而不是将其保存在XML中而成功地工作。显然,这不是我执行此批处理工作的方法。
答案 0 :(得分:1)
您可以使用Spring Expression Language访问作业参数。
#{jobParameters['build.timestamp']}
因此bean的定义将是这样。由于您正在使用文件系统资源,因此可以直接将文件注入到beanTaskletID
。因此,无需单独定义tempfiledir
bean。不要忘记添加范围步骤。
<bean id="[beanTaskletID]" class="[taskletDir]" scope="step">
<property name="temporaryFilesDir" value="file:${batch.file.drive}:/${batch.file.writer.input.root.directory}/${batch.input_file}/#{jobParameters['build.timestamp']}"/>
</bean>