我正在使用 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/