gpt4 book ai didi

java - 通过 JobParameter 在 Spring Batch 中设置属性文件

转载 作者:行者123 更新时间:2023-11-29 03:30:32 26 4
gpt4 key购买 nike

我在一个 Spring Batch 项目中有三个不同的 .properties 文件,我正在尝试设置哪个 .properties 文件应该用作 JobParameter。我希望能够像这样运行作业:

java CommandLineJobRunner context.xml jobName region=us

区域应指定应使用哪个 .properties 文件。问题是让上下文识别 JobParameter。我尝试了以下无济于事:

<context:property-placeholder location="classpath:batch.#{jobParameters['region']}.properties"/>

还有:

<util:properties id="batchProperties" location="classpath:batch.#{jobParameters['region']}.properties"></util:properties>

我听说添加 scope="step"可以解决类似的问题,但我尝试将其添加到上述两种解决方案中,但仍然存在异常。

我想我错过了为什么我不能让它工作的基本想法,但我无法弄清楚那个想法是什么。

如果有人对实现此功能有任何建议和/或解释我以前的方法失败的原因,我将不胜感激。

最佳答案

这不是正确的方法(不可能做你想做的事)。
你必须认为 jobParameters仅在作业运行时可用,且仅适用于标有 scope="step" 的组合步骤(而不是 <context:property-placeholder><util:properties> 具有 step 属性)。
解决该问题的一种方法是在使用监听器运行第一步之前在作业的执行上下文中加载属性文件:

public class PropertyLoaderJobExecutionListener extends StepExecutionListenerSupport {
Properties countryProperties;

public void setCountryProperties(Properties pfb) {
this.countryProperties = pfb;
}

@Override
public void beforeStep(StepExecution stepExecution) {
super.beforeStep(stepExecution);
// Store property file content in jobExecutionContext with name "batchProperties"
stepExecution.getJobExecution().getExecutionContext().put("batchProperties", countryProperties);
}
}

在你的 job.xml 中

<bean id="loadPropertiesListener" class="PropertyLoaderJobExecutionListener" scope="step">
<property name="pfb">
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:batch.#{jobParameters['region']}.properties" />
</bean>
</property>
</bean>

并在您的第一步中注册此监听器(您不能在您的 JobExectionListsner.beforeJob() 中执行此操作,因为目前没有 scope="job",并且 #{jobParameters['region']} 值的后期绑定(bind)不可用)。

要使用 spEL 访问您的数据,请使用以下语法:

#{jobExecutionContext.get('batchProperties').getProperty('language')}

或访问属性的更好语法(IDK spEL 太好了,抱歉)。

希望能说清楚,能帮助解决你的问题。

编辑(我的工作 job.xml 的完整代码):

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-util-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<job id="sourceJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet ref="getRemoteFileTasklet" />
<listeners>
<listener ref="loadPropertiesListener" />
</listeners>
</step>
</job>
<bean id="loadPropertiesListener" class="PropertyLoaderJobExecutionListener" scope="step">
<property name="pfb">
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:batch.#{jobParameters['region']}.properties" />
</bean>
</property>
</bean>
<bean id="getRemoteFileTasklet" class="GetRemoteFileTasklet" />

关于java - 通过 JobParameter 在 Spring Batch 中设置属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18516012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com