在 Spring Batch 的配置 xml 文件中动态替换占位符的值 [英] dynamically replace the value to placeholder in config xml file in Spring Batch

查看:37
本文介绍了在 Spring Batch 的配置 xml 文件中动态替换占位符的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring Batch 2 版本.我正在使用 JdbcCursorItemReader 从数据库读取数据.我已成功获取数据并将其写入文件.

I am using Spring Batch 2 version. I am reading data from database using JdbcCursorItemReader.I have successfully fetched the data and also written it to a file.

下面是Job.xml文件::

<bean id="itemReader"
        class="org.springframework.batch.item.database.JdbcCursorItemReader"
        scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql"
        value="select u.ID, u.USER_LOGIN, u.PASSWORD, u.AGE from USERS u" />
        </property>
        <property name="rowMapper">
           <bean class="com.example.UserRowMapper" />
        </property>
    </bean>

但问题是,我的查询非常大,所以我只想将该查询保留在 xml 文件之外,并从其他文件或属性文件(.property、yaml 或 xml)中获取该查询.

But the issue is,my query is quite big so I just want to keep that query out of xml file and get that query from other file or property file(.property,yaml or xml).

这样我就可以编写如下的xml代码::

So that I can write xml code as below::

<bean id="itemReader"               class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
                <property name="dataSource" ref="dataSource" />
                <property name="sql" value="$sql_query" />
                </property><property name="rowMapper">
                <bean class="com.example.UserRowMapper" />
                </property>
</bean>

实现这一目标的最佳方法是什么?

What is best way to achieve this?

推荐答案

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     
        <property name="locations">
            <list>
                <value>path1.properties</value>
                <value>path2.properties</value>
                .....
            </list>
        </property>     
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
</bean> 

...

<bean id="itemReader"  class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
                <property name="dataSource" ref="dataSource" />
                <property name="sql" value="${sql_query}" />
                </property><property name="rowMapper">
                <bean class="com.example.UserRowMapper" />
                </property>
</bean> 


path1.properties:
sql_query=value

PropertySourcesPlaceholderConfigurer 在 3.1 及更高版本中优先,而不是 PropertyPlaceholderConfigurer

PropertySourcesPlaceholderConfigurer is preffered in 3.1 and higher, instead of PropertyPlaceholderConfigurer

这篇关于在 Spring Batch 的配置 xml 文件中动态替换占位符的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆