java - 在 Spring Batch 中将值动态替换为配置 xml 文件中的占位符

标签 java xml spring spring-batch spring-bean

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

下面是在 Job.xml 文件中定义的 itemReader bean:

<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)中获取该查询。

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

<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>

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

最佳答案

<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

关于java - 在 Spring Batch 中将值动态替换为配置 xml 文件中的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41033571/

相关文章:

xml - 我如何在 Perl 中发送这个 SOAP XML?

xml - 从 XML 字符串而不是文件加载 Extjs 网格

java - 用spring初始化和释放全局资源

java - Spring LDAP 将用户添加到组

java - 将 javafx.scene.image.Image 写入文件?

java - 插入数据的唯一约束错误

javascript - ajax - 只返回响应文本的一部分

Spring Boot获取401未经授权的状态代码以进行简单的获取请求

java - java.lang.NoClassDefFoundError:用于gradle JUNIT测试的scala/Product $ class

java - 如何从 github 库构建 gradle jar