首页 文章

spring-batch:加载并使用属性文件

提问于
浏览
0

我是Spring批次的新人,所以我在这里提出一些基本的建议 .

在Spring Job / step运行时,在内存(或bean)中加载配置文件并使用其内容的最佳方法是什么?

我不确定,但基于一些谷歌搜索,我发现下面的情况,即使我不太明白为什么我应该定义一个作家,即使我不需要它:

  • step1:加载配置文件(内容是由=分隔的两个字段)

  • step2:执行一些java代码并使用以前的配置文件

所以对于第1步:

<bean id="inputFile" class="org.springframework.core.io.FileSystemResource" scope="step">
    <constructor-arg value="path_config_file"/>
</bean>

<bean id="readerConfigFile" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <property name="resource" ref="inputFile"/>
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="field,value"/>
                    <property name="delimiter" value="="/>
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="configProperties"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>

<bean id="outputConfig" class="outputConfig"></bean>

<bean id="configProperties" class="configProperties" scope="prototype"/>

所以我的问题是:

  • 如何使用文件中收集的信息?我应该把它们放在Java bean中吗?

  • 如何在不同步骤之间传递此信息或使其在整个应用程序生命周期中持久存在?

  • 您是否建议使用itemProcessor来实现上述目标?

任何建议都非常受欢迎

1 回答

  • 2

    我对你的问题有点困惑,因为我认为你只需要使用 PropertiesFactoryBean 在spring上下文中加载属性文件:

    <bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="location">
            <value>file:path_config_file</value>
          </property>
        </bean>
    
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="propertiesArray">
            <list>
              <ref bean="config"/>
            </list>
          </property>
        </bean>
    

    并且您可以使用$ {}占位符来引用属性值;但这种解决方案与 spring 批次无关;我不需要它!

    关于你的问题:

    • 使用POJO是一种好方法,因为spring-batch提供了内置映射策略(在您的情况下为 BeanWrapperFieldSetMapper

    • 作业中使用的对象只能在作业上下文中访问,而不能在应用程序上下文中访问(这就是为什么我认为您需要 PropertiesFactoryBean ) .
      要在步骤之间传递对象,请阅读How can we share data between the different steps of a Job in Spring Batch?
      如果需要将从 ItemReader<T> 读取的对象 T 转换为由 ItemWriter<S> 写入的S类型的对象,则会请求

    • ItemProcessor . 所以不,你不需要 ItemProcessor .

    我希望我很清楚,英语不是我的母语

相关问题