- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个 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/
我想验证文件名和记录。我的应用上下文如下 我的项目阅读器上下文如下所示
以下是阅读器bean的代码示例 有 .我有一个类似的作家 bean 。我想要一个处理器的动态切换器。界面ItemProcessor需求I & O值得一提的是,要么我必
我正在尝试将作业参数注入(inject)自定义 ItemReader。我已经查看了关于该主题的所有 StackOverflow 注释(示例:How to get access to job param
我要检索 JobParameter和 JobExecutionContext我的 ItemWriter 中的对象类(class)。 如何进行? 我尝试实现 StepExecutionListener通
我正在尝试设置一个基于 Spring Batch XML 的配置,该配置使用面向 block 的处理。我不想对提交间隔进行硬编码,因为对于我的应用程序来说,更合适的做法是允许从 JobParamete
我尝试将 @Conditional 条件与 Spring Batch 的 @StepScope 一起使用,以在 Condition 中使用 jobParameter,但在条件初始化期间,JobScop
我在一个 Spring Batch 项目中有三个不同的 .properties 文件,我正在尝试设置哪个 .properties 文件应该用作 JobParameter。我希望能够像这样运行作业: j
我打算编写一个 ASP.NET 页面来按需触发作业。目前,我正在使用 SimpleTrigger 类来触发作业,但 __Trigger 类中没有一个支持对象类型作为 JobParameters 中的值
我想在 spring JobParameters 中添加一个 boolean 类型的参数。但在这里:http://docs.spring.io/spring-batch/trunk/apidocs/o
这是一个非常简单的设置。我有一个可能包含 500 万条记录的文件,我需要读取这些记录并进行一些处理,然后将其发送到数据库。处理和写作的机制并不重要。我需要能够将路径和文件名 [/opt/etc/app
这是一个非常简单的设置。我有一个可能包含 500 万条记录的文件,我需要读取这些记录并进行一些处理,然后将其发送到数据库。处理和写作的机制并不重要。我需要能够将路径和文件名 [/opt/etc/app
我仍在研究 spring batch,遇到了一个场景,我需要将 jobParameter 传递给自定义 bean。作业参数包含一个文件的路径。 这是我的上下文的样子: 这已包含在此处未
我从链接 https://stackoverflow.com/q/15784984/814074 读到一个问题并尝试了上面链接中给出的解决方案。但是,我在运行代码时遇到以下错误: Error crea
我是 Spring Batch 的新手,我有一个以下主程序,我想使用 jobLauncherTestUtils 将其转换为测试用例。我们怎样才能做到这一点? 我关注了http://docs.sprin
如何将 JobParameters 传递给 MongoItemReader 查询? 我的 ItemReader 看起来像:- @Bean public ItemReader PersonTenantB
我正在尝试创建一个包含多个 FlatFileItemWriter 的 CustomWriter。配置是通过xml。我希望根据 jobParameters 中的值将 FlatFileItemWriter
我是一名优秀的程序员,十分优秀!